From c3ad489e5c1bc86bb62bee368e662a78d2df4343 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 30 Mar 2023 00:15:06 +0200 Subject: [PATCH 1/2] Update MetadataRoute to namespace --- .eslintrc.json | 2 +- .../metadata/resolve-route-data.test.ts | 6 +++--- .../loaders/metadata/resolve-route-data.ts | 19 ++++++++----------- .../lib/metadata/types/metadata-interface.ts | 8 ++++---- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index b40e889bddedd..b0e16775944ce 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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", diff --git a/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.test.ts b/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.test.ts index f24da86a19bcf..64c6692a5ed72 100644 --- a/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.test.ts +++ b/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.test.ts @@ -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 { @@ -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: '/' }, } diff --git a/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.ts b/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.ts index a149dc1755d69..26a74cca9a0dc 100644 --- a/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.ts +++ b/packages/next/src/build/webpack/loaders/metadata/resolve-route-data.ts @@ -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) { @@ -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 += '\n' content += '\n' @@ -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 '' } diff --git a/packages/next/src/lib/metadata/types/metadata-interface.ts b/packages/next/src/lib/metadata/types/metadata-interface.ts index 23762a6a5cb53..438c290bff62e 100644 --- a/packages/next/src/lib/metadata/types/metadata-interface.ts +++ b/packages/next/src/lib/metadata/types/metadata-interface.ts @@ -561,10 +561,10 @@ type SitemapFile = Array<{ }> type ResolvingMetadata = Promise -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 } From 9455b46b0f7058598bb17c556c26637629c1a9dc Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Thu, 30 Mar 2023 00:39:23 +0200 Subject: [PATCH 2/2] update test --- test/e2e/app-dir/metadata-dynamic-routes/app/manifest.ts | 2 +- test/e2e/app-dir/metadata-dynamic-routes/app/robots.ts | 2 +- test/e2e/app-dir/metadata-dynamic-routes/app/sitemap.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/e2e/app-dir/metadata-dynamic-routes/app/manifest.ts b/test/e2e/app-dir/metadata-dynamic-routes/app/manifest.ts index cff52e74ae12d..aa9acc113b498 100644 --- a/test/e2e/app-dir/metadata-dynamic-routes/app/manifest.ts +++ b/test/e2e/app-dir/metadata-dynamic-routes/app/manifest.ts @@ -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', diff --git a/test/e2e/app-dir/metadata-dynamic-routes/app/robots.ts b/test/e2e/app-dir/metadata-dynamic-routes/app/robots.ts index 80436c28dc90d..ed0b3cdf7d272 100644 --- a/test/e2e/app-dir/metadata-dynamic-routes/app/robots.ts +++ b/test/e2e/app-dir/metadata-dynamic-routes/app/robots.ts @@ -1,6 +1,6 @@ import type { MetadataRoute } from 'next' -export default function robots(): MetadataRoute['robots'] { +export default function robots(): MetadataRoute.Robots { return { rules: [ { diff --git a/test/e2e/app-dir/metadata-dynamic-routes/app/sitemap.ts b/test/e2e/app-dir/metadata-dynamic-routes/app/sitemap.ts index 7e609558e30c4..ff5e3ddff866f 100644 --- a/test/e2e/app-dir/metadata-dynamic-routes/app/sitemap.ts +++ b/test/e2e/app-dir/metadata-dynamic-routes/app/sitemap.ts @@ -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',