-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support static file robots.txt and sitemap.xml as metadata route (#46963
) Support top level static `robots.txt` and `sitemap.xml` as metadata route in app directory. When those files are placed in top root directory Refactored a bit the page files matching logic, to reuse it between dev server and build Closes NEXT-267
- Loading branch information
Showing
28 changed files
with
304 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/next/src/build/webpack/loaders/next-metadata-route-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type webpack from 'webpack' | ||
import path from 'path' | ||
|
||
const staticFileRegex = /[\\/](robots\.txt|sitemap\.xml)/ | ||
|
||
function isStaticRoute(resourcePath: string) { | ||
return staticFileRegex.test(resourcePath) | ||
} | ||
|
||
function getContentType(resourcePath: string) { | ||
const filename = path.basename(resourcePath) | ||
const [name] = filename.split('.') | ||
if (name === 'sitemap') return 'application/xml' | ||
if (name === 'robots') return 'text/plain' | ||
return 'text/plain' | ||
} | ||
|
||
const nextMetadataRouterLoader: webpack.LoaderDefinitionFunction = function ( | ||
content: string | ||
) { | ||
const { resourcePath } = this | ||
|
||
const code = isStaticRoute(resourcePath) | ||
? `import { NextResponse } from 'next/server' | ||
const content = ${JSON.stringify(content)} | ||
const contentType = ${JSON.stringify(getContentType(resourcePath))} | ||
export function GET() { | ||
return new NextResponse(content, { | ||
status: 200, | ||
headers: { | ||
'Content-Type': contentType, | ||
}, | ||
}) | ||
} | ||
export const dynamic = 'force-static' | ||
` | ||
: // TODO: handle the defined configs in routes file | ||
`export { default as GET } from ${JSON.stringify(resourcePath)}` | ||
|
||
return code | ||
} | ||
|
||
export default nextMetadataRouterLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
export function isAppRouteRoute(route: string): boolean { | ||
return route.endsWith('/route') | ||
} | ||
|
||
// TODO: support more metadata routes | ||
const staticMetadataRoutes = ['robots.txt', 'sitemap.xml'] | ||
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 | ||
const filename = route.replace(/^app\//, '').replace(/^\//, '') | ||
return staticMetadataRoutes.includes(filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.