Skip to content

Commit

Permalink
Revert "Support manifest.json static and dynamic route (vercel#47240)"
Browse files Browse the repository at this point in the history
This reverts commit e601a3b.
  • Loading branch information
anindosarker committed Mar 19, 2023
1 parent c08db77 commit a767a46
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 209 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/build/webpack/loaders/metadata/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ export const STATIC_METADATA_IMAGES = {
filename: 'twitter-image',
extensions: ['jpg', 'jpeg', 'png', 'gif'],
},
} as const
}

// Produce all compositions with filename (icon, apple-icon, etc.) with extensions (png, jpg, etc.)
async function enumMetadataFiles(
dir: string,
filename: string,
extensions: readonly string[],
extensions: string[],
{
resolvePath,
loaderContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Robots } from '../../../../lib/metadata/types/metadata-interface'
import type { RobotsFile } from '../../../../lib/metadata/types/metadata-interface'
import { resolveRobots, resolveSitemap } from './resolve-route-data'

describe('resolveRouteData', () => {
Expand Down Expand Up @@ -30,7 +30,7 @@ describe('resolveRouteData', () => {
})

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

const data2: Robots = {
const data2: RobotsFile = {
rules: {
// Can skip userAgent for single Robots
// @ts-expect-error When apply only 1 rule, only '*' or undefined is allowed
userAgent: 'Somebot',
allow: '/',
},
}

const data3: Robots = {
const data3: RobotsFile = {
rules: { allow: '/' },
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type {
Robots,
Sitemap,
RobotsFile,
SitemapFile,
} from '../../../../lib/metadata/types/metadata-interface'
import type { Manifest } from '../../../../lib/metadata/types/manifest-types'
import { resolveAsArrayOrUndefined } from '../../../../lib/metadata/generate/utils'

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

// TODO-METADATA: support multi sitemap files
// convert sitemap data to xml string
export function resolveSitemap(data: Sitemap): string {
export function resolveSitemap(data: SitemapFile): 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 @@ -61,22 +60,15 @@ export function resolveSitemap(data: Sitemap): string {
return content
}

export function resolveManifest(data: Manifest): string {
return JSON.stringify(data)
}

export function resolveRouteData(
data: Robots | Sitemap | Manifest,
fileType: 'robots' | 'sitemap' | 'manifest'
data: RobotsFile | SitemapFile,
fileType: 'robots' | 'sitemap'
): string {
if (fileType === 'robots') {
return resolveRobots(data as Robots)
return resolveRobots(data as RobotsFile)
}
if (fileType === 'sitemap') {
return resolveSitemap(data as Sitemap)
}
if (fileType === 'manifest') {
return resolveManifest(data as Manifest)
return resolveSitemap(data as SitemapFile)
}
return ''
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function getContentType(resourcePath: string) {
if (name === 'favicon' && ext === 'ico') return 'image/x-icon'
if (name === 'sitemap') return 'application/xml'
if (name === 'robots') return 'text/plain'
if (name === 'manifest') return 'application/manifest+json'

if (ext === 'png' || ext === 'jpeg' || ext === 'ico' || ext === 'svg') {
return imageExtMimeTypeMap[ext]
Expand Down
3 changes: 0 additions & 3 deletions packages/next/src/lib/metadata/get-metadata-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export function normalizeMetadataRoute(page: string) {
if (route === '/robots') {
route += '.txt'
}
if (route === '/manifest') {
route += '.webmanifest'
}
route = `${route}/route`
}
return route
Expand Down
17 changes: 4 additions & 13 deletions packages/next/src/lib/metadata/is-metadata-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { STATIC_METADATA_IMAGES } from '../../build/webpack/loaders/metadata/dis
// TODO-METADATA: support more metadata routes with more extensions
const defaultExtensions = ['js', 'jsx', 'ts', 'tsx']

const getExtensionRegexString = (extensions: readonly string[]) =>
const getExtensionRegexString = (extensions: string[]) =>
`(?:${extensions.join('|')})`

// When you only pass the file extension as `[]`, it will only match the static convention files
// e.g. /robots.txt, /sitemap.xml, /favicon.ico, /manifest.json
// e.g. /robots.txt, /sitemap.xml, /favicon.ico
// When you pass the file extension as `['js', 'jsx', 'ts', 'tsx']`, it will also match the dynamic convention files
// e.g. /robots.js, /sitemap.tsx, /favicon.jsx, /manifest.ts
// e.g. /robots.js, /sitemap.tsx, /favicon.jsx
// When `withExtension` is false, it will match the static convention files without the extension, by default it's true
// e.g. /robots, /sitemap, /favicon, /manifest, use to match dynamic API routes like app/robots.ts
// e.g. /robots, /sitemap, /favicon, use to match dynamic API routes like app/robots.ts
export function isMetadataRouteFile(
appDirRelativePath: string,
pageExtensions: string[],
Expand All @@ -33,15 +33,6 @@ export function isMetadataRouteFile(
: ''
}`
),
new RegExp(
`^[\\\\/]manifest${
withExtension
? `\\.${getExtensionRegexString(
pageExtensions.concat('webmanifest', 'json')
)}`
: ''
}`
),
new RegExp(`^[\\\\/]favicon\\.ico$`),
// TODO-METADATA: add dynamic routes for metadata images
new RegExp(
Expand Down
86 changes: 0 additions & 86 deletions packages/next/src/lib/metadata/types/manifest-types.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/next/src/lib/metadata/types/metadata-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ type RobotsFile = {
// Apply rules for all
rules:
| {
userAgent?: string | string[]
userAgent?: undefined | '*'
allow?: string | string[]
disallow?: string | string[]
crawlDelay?: number
Expand All @@ -554,10 +554,10 @@ type RobotsFile = {
host?: string
}

type Sitemap = Array<{
type SitemapFile = Array<{
url: string
lastModified?: string | Date
}>

export type ResolvingMetadata = Promise<ResolvedMetadata>
export { Metadata, ResolvedMetadata, RobotsFile as Robots, Sitemap }
export { Metadata, ResolvedMetadata, RobotsFile, SitemapFile }
1 change: 0 additions & 1 deletion packages/next/src/server/lib/find-page-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export function createValidFileMatcher(
* /robots.txt|<ext>
* /sitemap.xml|<ext>
* /favicon.ico
* /manifest.json|<ext>
* <route>/icon.png|jpg|<ext>
* <route>/apple-touch-icon.png|jpg|<ext>
*
Expand Down
7 changes: 5 additions & 2 deletions packages/next/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ export type ServerRuntime = 'nodejs' | 'experimental-edge' | 'edge' | undefined
// @ts-ignore This path is generated at build time and conflicts otherwise
export { NextConfig } from '../dist/server/config'

// @ts-ignore This path is generated at build time and conflicts otherwise
export type { Metadata } from '../dist/lib/metadata/types/metadata-interface'
export type {
Metadata,
RobotsFile,
SitemapFile, // @ts-ignore This path is generated at build time and conflicts otherwise
} from '../dist/lib/metadata/types/metadata-interface'

// Extend the React types with missing properties
declare module 'react' {
Expand Down
18 changes: 0 additions & 18 deletions test/e2e/app-dir/metadata-dynamic-routes/app/manifest.ts

This file was deleted.

4 changes: 3 additions & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/app/robots.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default function robots() {
import type { RobotsFile } from 'next'

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

export default function sitemap(): SitemapFile {
return [
{
url: 'https://example.com',
Expand Down
29 changes: 0 additions & 29 deletions test/e2e/app-dir/metadata-dynamic-routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,6 @@ createNextDescribe(
"
`)
})

it('should handle manifest.[ext] dynamic routes', async () => {
const res = await next.fetch('/manifest.webmanifest')
const json = await res.json()

expect(res.headers.get('content-type')).toBe(
'application/manifest+json'
)
expect(res.headers.get('cache-control')).toBe(
'public, max-age=0, must-revalidate'
)

expect(json).toMatchObject({
name: 'Next.js App',
short_name: 'Next.js App',
description: 'Next.js App',
start_url: '/',
display: 'standalone',
background_color: '#fff',
theme_color: '#fff',
icons: [
{
src: '/favicon.ico',
sizes: 'any',
type: 'image/x-icon',
},
],
})
})
})
}
)
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata/app/basic/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const metadata: Metadata = {
authors: [{ name: 'huozhi' }, { name: 'tree', url: 'https://tree.com' }],
themeColor: { color: 'cyan', media: '(prefers-color-scheme: dark)' },
colorScheme: 'dark',
manifest: 'https://www.google.com/manifest',
manifest: 'https://github.com/manifest.json',
viewport: {
width: 'device-width',
initialScale: 1,
Expand Down
9 changes: 0 additions & 9 deletions test/e2e/app-dir/metadata/app/manifest.webmanifest

This file was deleted.

Loading

0 comments on commit a767a46

Please sign in to comment.