Unverified Commit fd6b93d4 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Generate fixed route path for favicon.ico (#46997)

Generate `/favicon.ico` route when favicon.ico is placed into `app/`.

Still collect favicon metadata image information through
metadata-image-loader but don't emit the file to static dist anymore.
Also collect favicon through metadata routes, and render it as static
routes. Also remove the `hash` we generated before, not needed anymore.

Change metadata static routes rendering process: collect static metadata
assets, read the buffer of the file data and return it in the response.

Closes NEXT-791
parent d200e5fa
import type webpack from 'webpack' import type webpack from 'webpack'
import type { AppLoaderOptions } from '../next-app-loader' import type { AppLoaderOptions } from '../next-app-loader'
import type { CollectingMetadata } from './types' import type {
CollectingMetadata,
PossibleImageFileNameConvention,
} from './types'
import path from 'path' import path from 'path'
import { stringify } from 'querystring' import { stringify } from 'querystring'
type PossibleImageFileNameConvention =
| 'icon'
| 'apple'
| 'favicon'
| 'twitter'
| 'opengraph'
const METADATA_TYPE = 'metadata' const METADATA_TYPE = 'metadata'
export const METADATA_IMAGE_RESOURCE_QUERY = '?__next_metadata' export const METADATA_IMAGE_RESOURCE_QUERY = '?__next_metadata'
...@@ -127,6 +123,7 @@ export async function discoverStaticMetadataFiles( ...@@ -127,6 +123,7 @@ export async function discoverStaticMetadataFiles(
...metadataImageLoaderOptions, ...metadataImageLoaderOptions,
numericSizes: numericSizes:
type === 'twitter' || type === 'opengraph' ? '1' : undefined, type === 'twitter' || type === 'opengraph' ? '1' : undefined,
type,
})}!` + })}!` +
filepath + filepath +
METADATA_IMAGE_RESOURCE_QUERY METADATA_IMAGE_RESOURCE_QUERY
......
...@@ -31,3 +31,10 @@ export type MetadataImageModule = { ...@@ -31,3 +31,10 @@ export type MetadataImageModule = {
height?: number height?: number
} }
) )
export type PossibleImageFileNameConvention =
| 'icon'
| 'apple'
| 'favicon'
| 'twitter'
| 'opengraph'
import type { MetadataImageModule } from './metadata/types' import type {
MetadataImageModule,
PossibleImageFileNameConvention,
} from './metadata/types'
import loaderUtils from 'next/dist/compiled/loader-utils3' import loaderUtils from 'next/dist/compiled/loader-utils3'
import { getImageSize } from '../../../server/image-optimizer' import { getImageSize } from '../../../server/image-optimizer'
...@@ -6,6 +9,7 @@ interface Options { ...@@ -6,6 +9,7 @@ interface Options {
isDev: boolean isDev: boolean
assetPrefix: string assetPrefix: string
numericSizes: boolean numericSizes: boolean
type: PossibleImageFileNameConvention
} }
const mimeTypeMap = { const mimeTypeMap = {
...@@ -13,24 +17,26 @@ const mimeTypeMap = { ...@@ -13,24 +17,26 @@ const mimeTypeMap = {
png: 'image/png', png: 'image/png',
ico: 'image/x-icon', ico: 'image/x-icon',
svg: 'image/svg+xml', svg: 'image/svg+xml',
avif: 'image/avif',
webp: 'image/webp',
} }
async function nextMetadataImageLoader(this: any, content: Buffer) { async function nextMetadataImageLoader(this: any, content: Buffer) {
const options: Options = this.getOptions() const options: Options = this.getOptions()
const { assetPrefix, isDev, numericSizes } = options const { assetPrefix, isDev, numericSizes, type } = options
const context = this.rootContext const context = this.rootContext
const opts = { context, content } const opts = { context, content }
// favicon is the special case, always generate '/favicon.ico'
const isFavIcon = type === 'favicon'
// e.g. icon.png -> server/static/media/metadata/icon.399de3b9.png // e.g. icon.png -> server/static/media/metadata/icon.399de3b9.png
const interpolatedName = loaderUtils.interpolateName( const interpolatedName = loaderUtils.interpolateName(
this, this,
'/static/media/metadata/[name].[hash:8].[ext]', (isFavIcon ? '' : '/static/media/metadata/') + '[name].[ext]',
opts opts
) )
const outputPath = assetPrefix + '/_next' + interpolatedName const outputPath = isFavIcon
? '/' + interpolatedName
: assetPrefix + '/_next' + interpolatedName
let extension = loaderUtils.interpolateName(this, '[ext]', opts) let extension = loaderUtils.interpolateName(this, '[ext]', opts)
if (extension === 'jpg') { if (extension === 'jpg') {
extension = 'jpeg' extension = 'jpeg'
...@@ -61,7 +67,10 @@ async function nextMetadataImageLoader(this: any, content: Buffer) { ...@@ -61,7 +67,10 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
const stringifiedData = JSON.stringify(imageData) const stringifiedData = JSON.stringify(imageData)
// TODO-METADATA: Move image generation to static app routes
if (!isFavIcon) {
this.emitFile(`../${isDev ? '' : '../'}${interpolatedName}`, content, null) this.emitFile(`../${isDev ? '' : '../'}${interpolatedName}`, content, null)
}
return `export default ${stringifiedData};` return `export default ${stringifiedData};`
} }
......
import type webpack from 'webpack' import type webpack from 'webpack'
import path from 'path' import path from 'path'
import { isStaticMetadataRoute } from '../../../lib/is-app-route-route'
const staticFileRegex = /[\\/](robots\.txt|sitemap\.xml)/
function isStaticRoute(resourcePath: string) {
return staticFileRegex.test(resourcePath)
}
function getContentType(resourcePath: string) { function getContentType(resourcePath: string) {
const filename = path.basename(resourcePath) const filename = path.basename(resourcePath)
const [name] = filename.split('.') const [name, ext] = filename.split('.')
if (name === 'favicon' && ext === 'ico') return 'image/x-icon'
if (name === 'sitemap') return 'application/xml' if (name === 'sitemap') return 'application/xml'
if (name === 'robots') return 'text/plain' if (name === 'robots') return 'text/plain'
return 'text/plain' return 'text/plain'
} }
const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction = function ( // `import.meta.url` is the resource name of the current module.
content: string // When it's static route, it could be favicon.ico, sitemap.xml, robots.txt etc.
) { const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction = function () {
const { resourcePath } = this const { resourcePath } = this
const code = isStaticRoute(resourcePath) const code = isStaticMetadataRoute(resourcePath)
? `import { NextResponse } from 'next/server' ? `import { NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const content = ${JSON.stringify(content)} const buffer = fs.readFileSync(fileURLToPath(import.meta.url))
const contentType = ${JSON.stringify(getContentType(resourcePath))} const contentType = ${JSON.stringify(getContentType(resourcePath))}
export function GET() { export function GET() {
return new NextResponse(content, { return new NextResponse(buffer, {
status: 200, status: 200,
headers: { headers: {
'Content-Type': contentType, 'Content-Type': contentType,
......
export function stringifyRequest(loaderContext: any, request: any) { import type webpack from 'webpack'
export function stringifyRequest(
loaderContext: webpack.LoaderContext<any>,
request: string
) {
return JSON.stringify( return JSON.stringify(
loaderContext.utils.contextify( loaderContext.utils.contextify(
loaderContext.context || loaderContext.rootContext, loaderContext.context || loaderContext.rootContext,
......
import path from '../shared/lib/isomorphic/path'
export function isAppRouteRoute(route: string): boolean { export function isAppRouteRoute(route: string): boolean {
return route.endsWith('/route') return route.endsWith('/route')
} }
// TODO: support more metadata routes // Match routes that are metadata routes, e.g. /sitemap.xml, /favicon.<ext>, /<icon>.<ext>, etc.
const staticMetadataRoutes = ['robots.txt', 'sitemap.xml'] // TODO-METADATA: support more metadata routes with more extensions
const staticMetadataRoutes = ['robots.txt', 'sitemap.xml', 'favicon.ico']
export function isMetadataRoute(route: string): boolean { export function isMetadataRoute(route: string): boolean {
// Remove the 'app/' or '/' prefix, only check the route name since they're only allowed in root app directory // Remove the 'app/' or '/' prefix, only check the route name since they're only allowed in root app directory
const filename = route.replace(/^app\//, '').replace(/^\//, '') const filename = route.replace(/^app\//, '').replace(/^\//, '')
return staticMetadataRoutes.includes(filename) return staticMetadataRoutes.includes(filename)
} }
// Only match the static metadata files
// TODO-METADATA: support static metadata files under nested routes folders
export function isStaticMetadataRoute(resourcePath: string) {
const filename = path.basename(resourcePath)
return staticMetadataRoutes.includes(filename)
}
...@@ -33,7 +33,7 @@ export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvid ...@@ -33,7 +33,7 @@ export class DevAppRouteRouteMatcherProvider extends FileCacheRouteMatcherProvid
.concat('txt') .concat('txt')
.join('|')})?$|[/\\\\]sitemap\\.(?:${extensions .join('|')})?$|[/\\\\]sitemap\\.(?:${extensions
.concat('xml') .concat('xml')
.join('|')})?$` .join('|')})?$|[/\\\\]favicon\\.ico$`
) )
const pageNormalizer = new AbsoluteFilenameNormalizer(appDir, extensions) const pageNormalizer = new AbsoluteFilenameNormalizer(appDir, extensions)
......
...@@ -79,19 +79,33 @@ export function createValidFileMatcher( ...@@ -79,19 +79,33 @@ export function createValidFileMatcher(
pageExtensions: string[], pageExtensions: string[],
appDirPath: string | undefined appDirPath: string | undefined
) { ) {
const getExtensionRegexString = (
extensions: string[],
...extraExtensions: string[]
) => `(?:${extensions.concat(extraExtensions).join('|')})`
const validExtensionFileRegex = new RegExp( const validExtensionFileRegex = new RegExp(
`\\.+(?:${pageExtensions.join('|')})$` '\\.' + getExtensionRegexString(pageExtensions) + '$'
) )
const leafOnlyPageFileRegex = new RegExp( const leafOnlyPageFileRegex = new RegExp(
`(^(page|route)|[\\\\/](page|route))\\.(?:${pageExtensions.join('|')})$` `(^(page|route)|[\\\\/](page|route))\\.${getExtensionRegexString(
pageExtensions
)}$`
) )
// TODO: support other metadata routes /** TODO-METADATA: support other metadata routes
// regex for /robots.txt|((j|t)sx?) * regex for:
// regex for /sitemap.xml|((j|t)sx?) *
* /robots.txt|((j|t)sx?)
* /sitemap.xml|((j|t)sx?)
* /favicon.ico
*
*/
const metadataRoutesRelativePathRegex = new RegExp( const metadataRoutesRelativePathRegex = new RegExp(
`^[\\\\/](robots)\\.(?:${pageExtensions.concat('txt').join('|')})$` + `^[\\\\/]((robots\\.${getExtensionRegexString(pageExtensions, 'txt')})` +
'|' +
`(sitemap\\.${getExtensionRegexString(pageExtensions, 'xml')})` +
'|' + '|' +
`^[\\\\/](sitemap)\\.(?:${pageExtensions.concat('xml').join('|')})$` `(favicon\\.ico))$`
) )
function isMetadataRouteFile(filePath: string) { function isMetadataRouteFile(filePath: string) {
......
...@@ -428,7 +428,7 @@ createNextDescribe( ...@@ -428,7 +428,7 @@ createNextDescribe(
it('should pick up opengraph-image and twitter-image as static metadata files', async () => { it('should pick up opengraph-image and twitter-image as static metadata files', async () => {
const $ = await next.render$('/opengraph/static') const $ = await next.render$('/opengraph/static')
expect($('[property="og:image"]').attr('content')).toMatch( expect($('[property="og:image"]').attr('content')).toMatch(
/https:\/\/example.com\/_next\/static\/media\/metadata\/opengraph-image.\w+.png/ /https:\/\/example.com\/_next\/static\/media\/metadata\/opengraph-image.png/
) )
expect($('[property="og:image:type"]').attr('content')).toBe( expect($('[property="og:image:type"]').attr('content')).toBe(
'image/png' 'image/png'
...@@ -437,7 +437,7 @@ createNextDescribe( ...@@ -437,7 +437,7 @@ createNextDescribe(
expect($('[property="og:image:height"]').attr('content')).toBe('114') expect($('[property="og:image:height"]').attr('content')).toBe('114')
expect($('[name="twitter:image"]').attr('content')).toMatch( expect($('[name="twitter:image"]').attr('content')).toMatch(
/https:\/\/example.com\/_next\/static\/media\/metadata\/twitter-image.\w+.png/ /https:\/\/example.com\/_next\/static\/media\/metadata\/twitter-image.png/
) )
expect($('[name="twitter:card"]').attr('content')).toBe( expect($('[name="twitter:card"]').attr('content')).toBe(
'summary_large_image' 'summary_large_image'
...@@ -445,9 +445,7 @@ createNextDescribe( ...@@ -445,9 +445,7 @@ createNextDescribe(
// favicon shouldn't be overridden // favicon shouldn't be overridden
const $icon = $('link[rel="icon"]') const $icon = $('link[rel="icon"]')
expect($icon.attr('href')).toMatch( expect($icon.attr('href')).toBe('/favicon.ico')
/_next\/static\/media\/metadata\/favicon.\w+.ico/
)
}) })
}) })
...@@ -497,17 +495,13 @@ createNextDescribe( ...@@ -497,17 +495,13 @@ createNextDescribe(
it('should support root level of favicon.ico', async () => { it('should support root level of favicon.ico', async () => {
let $ = await next.render$('/') let $ = await next.render$('/')
let $icon = $('link[rel="icon"]') let $icon = $('link[rel="icon"]')
expect($icon.attr('href')).toMatch( expect($icon.attr('href')).toBe('/favicon.ico')
/_next\/static\/media\/metadata\/favicon.\w+.ico/
)
expect($icon.attr('type')).toBe('image/x-icon') expect($icon.attr('type')).toBe('image/x-icon')
expect($icon.attr('sizes')).toBe('any') expect($icon.attr('sizes')).toBe('any')
$ = await next.render$('/basic') $ = await next.render$('/basic')
$icon = $('link[rel="icon"]') $icon = $('link[rel="icon"]')
expect($icon.attr('href')).toMatch( expect($icon.attr('href')).toBe('/favicon.ico')
/_next\/static\/media\/metadata\/favicon.\w+.ico/
)
expect($icon.attr('sizes')).toBe('any') expect($icon.attr('sizes')).toBe('any')
}) })
}) })
...@@ -520,12 +514,12 @@ createNextDescribe( ...@@ -520,12 +514,12 @@ createNextDescribe(
const $appleIcon = $('head > link[rel="apple-touch-icon"]') const $appleIcon = $('head > link[rel="apple-touch-icon"]')
expect($icon.attr('href')).toMatch( expect($icon.attr('href')).toMatch(
/\/_next\/static\/media\/metadata\/icon1\.\w+\.png/ /\/_next\/static\/media\/metadata\/icon1\.png/
) )
expect($icon.attr('sizes')).toBe('32x32') expect($icon.attr('sizes')).toBe('32x32')
expect($icon.attr('type')).toBe('image/png') expect($icon.attr('type')).toBe('image/png')
expect($appleIcon.attr('href')).toMatch( expect($appleIcon.attr('href')).toMatch(
/\/_next\/static\/media\/metadata\/apple-icon\.\w+\.png/ /\/_next\/static\/media\/metadata\/apple-icon\.png/
) )
expect($appleIcon.attr('type')).toBe('image/png') expect($appleIcon.attr('type')).toBe('image/png')
expect($appleIcon.attr('sizes')).toMatch('114x114') expect($appleIcon.attr('sizes')).toMatch('114x114')
...@@ -538,7 +532,7 @@ createNextDescribe( ...@@ -538,7 +532,7 @@ createNextDescribe(
const $appleIcon = $('head > link[rel="apple-touch-icon"]') const $appleIcon = $('head > link[rel="apple-touch-icon"]')
expect($icon.attr('href')).toMatch( expect($icon.attr('href')).toMatch(
/\/_next\/static\/media\/metadata\/icon\.\w+\.png/ /\/_next\/static\/media\/metadata\/icon\.png/
) )
expect($icon.attr('sizes')).toBe('114x114') expect($icon.attr('sizes')).toBe('114x114')
...@@ -556,7 +550,7 @@ createNextDescribe( ...@@ -556,7 +550,7 @@ createNextDescribe(
const $ = await next.render$('/icons/static') const $ = await next.render$('/icons/static')
const $icon = $('head > link[rel="icon"][type!="image/x-icon"]') const $icon = $('head > link[rel="icon"][type!="image/x-icon"]')
return $icon.attr('href') return $icon.attr('href')
}, /\/_next\/static\/media\/metadata\/icon2\.\w+\.png/) }, /\/_next\/static\/media\/metadata\/icon2\.png/)
await next.renameFile( await next.renameFile(
'app/icons/static/icon2.png', 'app/icons/static/icon2.png',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment