Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MetadataRoute to namespace #47674

Merged
merged 5 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@typescript-eslint/consistent-type-assertions": "warn",
"no-array-constructor": "off",
"@typescript-eslint/no-array-constructor": "warn",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-namespace": "off",
"no-use-before-define": "off",
"@typescript-eslint/no-use-before-define": [
"warn",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('resolveRouteData', () => {
})

it('should error with ts when specify both wildcard userAgent and specific userAgent', () => {
const data1: MetadataRoute['robots'] = {
const data1: MetadataRoute.Robots = {
rules: [
// @ts-expect-error userAgent is required for Array<Robots>
{
Expand All @@ -43,14 +43,14 @@ describe('resolveRouteData', () => {
],
}

const data2: MetadataRoute['robots'] = {
const data2: MetadataRoute.Robots = {
rules: {
// Can skip userAgent for single Robots
allow: '/',
},
}

const data3: MetadataRoute['robots'] = {
const data3: MetadataRoute.Robots = {
rules: { allow: '/' },
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MetadataRoute } from '../../../../lib/metadata/types/metadata-inte
import { resolveArray } from '../../../../lib/metadata/generate/utils'

// convert robots data to txt string
export function resolveRobots(data: MetadataRoute['robots']): string {
export function resolveRobots(data: MetadataRoute.Robots): string {
let content = ''
const rules = Array.isArray(data.rules) ? data.rules : [data.rules]
for (const rule of rules) {
Expand Down Expand Up @@ -43,7 +43,7 @@ export function resolveRobots(data: MetadataRoute['robots']): string {

// TODO-METADATA: support multi sitemap files
// convert sitemap data to xml string
export function resolveSitemap(data: MetadataRoute['sitemap']): string {
export function resolveSitemap(data: MetadataRoute.Sitemap): string {
let content = ''
content += '<?xml version="1.0" encoding="UTF-8"?>\n'
content += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
Expand All @@ -63,25 +63,22 @@ export function resolveSitemap(data: MetadataRoute['sitemap']): string {
return content
}

export function resolveManifest(data: MetadataRoute['manifest']): string {
export function resolveManifest(data: MetadataRoute.Manifest): string {
return JSON.stringify(data)
}

export function resolveRouteData(
data:
| MetadataRoute['robots']
| MetadataRoute['sitemap']
| MetadataRoute['manifest'],
fileType: keyof MetadataRoute
data: MetadataRoute.Robots | MetadataRoute.Sitemap | MetadataRoute.Manifest,
fileType: 'robots' | 'sitemap' | 'manifest'
): string {
if (fileType === 'robots') {
return resolveRobots(data as MetadataRoute['robots'])
return resolveRobots(data as MetadataRoute.Robots)
}
if (fileType === 'sitemap') {
return resolveSitemap(data as MetadataRoute['sitemap'])
return resolveSitemap(data as MetadataRoute.Sitemap)
}
if (fileType === 'manifest') {
return resolveManifest(data as MetadataRoute['manifest'])
return resolveManifest(data as MetadataRoute.Manifest)
}
return ''
}
8 changes: 4 additions & 4 deletions packages/next/src/lib/metadata/types/metadata-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,10 @@ type SitemapFile = Array<{
}>

type ResolvingMetadata = Promise<ResolvedMetadata>
type MetadataRoute = {
robots: RobotsFile
sitemap: SitemapFile
manifest: ManifestFile
declare namespace MetadataRoute {
export type Robots = RobotsFile
export type Sitemap = SitemapFile
export type Manifest = ManifestFile
}

export { Metadata, ResolvedMetadata, ResolvingMetadata, MetadataRoute }
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/app/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MetadataRoute } from 'next'

export default function manifest(): MetadataRoute['manifest'] {
export default function manifest(): MetadataRoute.Manifest {
return {
name: 'Next.js App',
short_name: 'Next.js App',
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/app/robots.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MetadataRoute } from 'next'

export default function robots(): MetadataRoute['robots'] {
export default function robots(): MetadataRoute.Robots {
return {
rules: [
{
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MetadataRoute } from 'next'

export default function sitemap(): MetadataRoute['sitemap'] {
export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://example.com',
Expand Down