Unverified Commit 03ca164a authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure cache soft tags are correct (#57850)

We noticed that our internal soft tags were unexpectedly including the query string for SSR routes which causes the URL soft tag to not match correctly so this ensures we properly normalize the value to just the pathname and not include the query. 
parent 74153e12
...@@ -1387,6 +1387,7 @@ export default async function build( ...@@ -1387,6 +1387,7 @@ export default async function build(
async () => async () =>
hasCustomErrorPage && hasCustomErrorPage &&
pagesStaticWorkers.isPageStatic({ pagesStaticWorkers.isPageStatic({
dir,
page: '/_error', page: '/_error',
distDir, distDir,
configFileName, configFileName,
...@@ -1586,6 +1587,7 @@ export default async function build( ...@@ -1586,6 +1587,7 @@ export default async function build(
? appStaticWorkers ? appStaticWorkers
: pagesStaticWorkers : pagesStaticWorkers
)!.isPageStatic({ )!.isPageStatic({
dir,
page, page,
originalAppPath, originalAppPath,
distDir, distDir,
......
...@@ -1216,6 +1216,7 @@ export const collectGenerateParams = async ( ...@@ -1216,6 +1216,7 @@ export const collectGenerateParams = async (
} }
export async function buildAppStaticPaths({ export async function buildAppStaticPaths({
dir,
page, page,
distDir, distDir,
configFileName, configFileName,
...@@ -1229,6 +1230,7 @@ export async function buildAppStaticPaths({ ...@@ -1229,6 +1230,7 @@ export async function buildAppStaticPaths({
serverHooks, serverHooks,
ppr, ppr,
}: { }: {
dir: string
page: string page: string
configFileName: string configFileName: string
generateParams: GenerateParams generateParams: GenerateParams
...@@ -1252,7 +1254,9 @@ export async function buildAppStaticPaths({ ...@@ -1252,7 +1254,9 @@ export async function buildAppStaticPaths({
let CacheHandler: any let CacheHandler: any
if (incrementalCacheHandlerPath) { if (incrementalCacheHandlerPath) {
CacheHandler = require(incrementalCacheHandlerPath) CacheHandler = require(path.isAbsolute(incrementalCacheHandlerPath)
? incrementalCacheHandlerPath
: path.join(dir, incrementalCacheHandlerPath))
CacheHandler = CacheHandler.default || CacheHandler CacheHandler = CacheHandler.default || CacheHandler
} }
...@@ -1379,6 +1383,7 @@ export async function buildAppStaticPaths({ ...@@ -1379,6 +1383,7 @@ export async function buildAppStaticPaths({
} }
export async function isPageStatic({ export async function isPageStatic({
dir,
page, page,
distDir, distDir,
configFileName, configFileName,
...@@ -1396,6 +1401,7 @@ export async function isPageStatic({ ...@@ -1396,6 +1401,7 @@ export async function isPageStatic({
incrementalCacheHandlerPath, incrementalCacheHandlerPath,
ppr, ppr,
}: { }: {
dir: string
page: string page: string
distDir: string distDir: string
configFileName: string configFileName: string
...@@ -1565,6 +1571,7 @@ export async function isPageStatic({ ...@@ -1565,6 +1571,7 @@ export async function isPageStatic({
fallback: prerenderFallback, fallback: prerenderFallback,
encodedPaths: encodedPrerenderRoutes, encodedPaths: encodedPrerenderRoutes,
} = await buildAppStaticPaths({ } = await buildAppStaticPaths({
dir,
page, page,
serverHooks, serverHooks,
staticGenerationAsyncStorage, staticGenerationAsyncStorage,
......
...@@ -7,12 +7,15 @@ export function createIncrementalCache( ...@@ -7,12 +7,15 @@ export function createIncrementalCache(
incrementalCacheHandlerPath: string | undefined, incrementalCacheHandlerPath: string | undefined,
isrMemoryCacheSize: number | undefined, isrMemoryCacheSize: number | undefined,
fetchCacheKeyPrefix: string | undefined, fetchCacheKeyPrefix: string | undefined,
distDir: string distDir: string,
dir: string
) { ) {
// Custom cache handler overrides. // Custom cache handler overrides.
let CacheHandler: any let CacheHandler: any
if (incrementalCacheHandlerPath) { if (incrementalCacheHandlerPath) {
CacheHandler = require(incrementalCacheHandlerPath) CacheHandler = require(path.isAbsolute(incrementalCacheHandlerPath)
? incrementalCacheHandlerPath
: path.join(dir, incrementalCacheHandlerPath))
CacheHandler = CacheHandler.default || CacheHandler CacheHandler = CacheHandler.default || CacheHandler
} }
...@@ -38,7 +41,7 @@ export function createIncrementalCache( ...@@ -38,7 +41,7 @@ export function createIncrementalCache(
readFile: fs.promises.readFile, readFile: fs.promises.readFile,
readFileSync: fs.readFileSync, readFileSync: fs.readFileSync,
writeFile: (f, d) => fs.promises.writeFile(f, d), writeFile: (f, d) => fs.promises.writeFile(f, d),
mkdir: (dir) => fs.promises.mkdir(dir, { recursive: true }), mkdir: (d) => fs.promises.mkdir(d, { recursive: true }),
stat: (f) => fs.promises.stat(f), stat: (f) => fs.promises.stat(f),
}, },
serverDistDir: path.join(distDir, 'server'), serverDistDir: path.join(distDir, 'server'),
......
...@@ -674,6 +674,7 @@ export async function exportAppImpl( ...@@ -674,6 +674,7 @@ export async function exportAppImpl(
const result = await pageExportSpan.traceAsyncFn(async () => { const result = await pageExportSpan.traceAsyncFn(async () => {
return await exportPage({ return await exportPage({
dir,
path, path,
pathMap, pathMap,
distDir, distDir,
......
...@@ -35,6 +35,7 @@ type PathMap = ExportPathMap[keyof ExportPathMap] ...@@ -35,6 +35,7 @@ type PathMap = ExportPathMap[keyof ExportPathMap]
export interface ExportPageInput { export interface ExportPageInput {
path: string path: string
dir: string
pathMap: PathMap pathMap: PathMap
distDir: string distDir: string
outDir: string outDir: string
......
...@@ -45,6 +45,7 @@ async function exportPageImpl( ...@@ -45,6 +45,7 @@ async function exportPageImpl(
fileWriter: FileWriter fileWriter: FileWriter
): Promise<ExportRouteResult | undefined> { ): Promise<ExportRouteResult | undefined> {
const { const {
dir,
path, path,
pathMap, pathMap,
distDir, distDir,
...@@ -219,7 +220,8 @@ async function exportPageImpl( ...@@ -219,7 +220,8 @@ async function exportPageImpl(
incrementalCacheHandlerPath, incrementalCacheHandlerPath,
isrMemoryCacheSize, isrMemoryCacheSize,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
distDir distDir,
dir
) )
: undefined : undefined
......
...@@ -1048,6 +1048,7 @@ export const renderToHTMLOrFlight: AppPageRender = ( ...@@ -1048,6 +1048,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
query, query,
renderOpts renderOpts
) => { ) => {
// TODO: this includes query string, should it?
const pathname = validateURL(req.url) const pathname = validateURL(req.url)
return RequestAsyncStorageWrapper.wrap( return RequestAsyncStorageWrapper.wrap(
......
...@@ -656,6 +656,7 @@ export default class DevServer extends Server { ...@@ -656,6 +656,7 @@ export default class DevServer extends Server {
try { try {
const pathsResult = await staticPathsWorker.loadStaticPaths({ const pathsResult = await staticPathsWorker.loadStaticPaths({
dir: this.dir,
distDir: this.distDir, distDir: this.distDir,
pathname, pathname,
config: { config: {
......
...@@ -28,6 +28,7 @@ type RuntimeConfig = { ...@@ -28,6 +28,7 @@ type RuntimeConfig = {
// side-effects aren't relied on in dev that will break // side-effects aren't relied on in dev that will break
// during a production build // during a production build
export async function loadStaticPaths({ export async function loadStaticPaths({
dir,
distDir, distDir,
pathname, pathname,
config, config,
...@@ -43,6 +44,7 @@ export async function loadStaticPaths({ ...@@ -43,6 +44,7 @@ export async function loadStaticPaths({
incrementalCacheHandlerPath, incrementalCacheHandlerPath,
ppr, ppr,
}: { }: {
dir: string
distDir: string distDir: string
pathname: string pathname: string
config: RuntimeConfig config: RuntimeConfig
...@@ -101,6 +103,7 @@ export async function loadStaticPaths({ ...@@ -101,6 +103,7 @@ export async function loadStaticPaths({
: await collectGenerateParams(components.ComponentMod.tree) : await collectGenerateParams(components.ComponentMod.tree)
return await buildAppStaticPaths({ return await buildAppStaticPaths({
dir,
page: pathname, page: pathname,
generateParams, generateParams,
configFileName: config.configFileName, configFileName: config.configFileName,
......
...@@ -92,7 +92,9 @@ export function addImplicitTags(staticGenerationStore: StaticGenerationStore) { ...@@ -92,7 +92,9 @@ export function addImplicitTags(staticGenerationStore: StaticGenerationStore) {
} }
if (urlPathname) { if (urlPathname) {
const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${urlPathname}` const parsedPathname = new URL(urlPathname, 'http://n').pathname
const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${parsedPathname}`
if (!staticGenerationStore.tags?.includes(tag)) { if (!staticGenerationStore.tags?.includes(tag)) {
staticGenerationStore.tags.push(tag) staticGenerationStore.tags.push(tag)
} }
......
...@@ -6,6 +6,7 @@ export default async function Page({ params }) { ...@@ -6,6 +6,7 @@ export default async function Page({ params }) {
{ {
next: { next: {
tags: ['ssr-page'], tags: ['ssr-page'],
revalidate: 3,
}, },
} }
).then((res) => res.text()) ).then((res) => res.text())
......
...@@ -5,13 +5,17 @@ module.exports = class CacheHandler { ...@@ -5,13 +5,17 @@ module.exports = class CacheHandler {
console.log('initialized custom cache-handler') console.log('initialized custom cache-handler')
} }
async get(key) { async get(key, ctx) {
console.log('cache-handler get', key) console.log('cache-handler get', key, ctx)
if (ctx.softTags?.some((tag) => tag.includes('?'))) {
throw new Error(`invalid soft tag found should only be pathname`)
}
return cache.get(key) return cache.get(key)
} }
async set(key, data) { async set(key, data, ctx) {
console.log('cache-handler set', key) console.log('cache-handler set', key, ctx)
cache.set(key, { cache.set(key, {
value: data, value: data,
lastModified: Date.now(), lastModified: Date.now(),
......
...@@ -30,6 +30,7 @@ describe('required server files app router', () => { ...@@ -30,6 +30,7 @@ describe('required server files app router', () => {
files: { files: {
app: new FileRef(join(__dirname, 'app')), app: new FileRef(join(__dirname, 'app')),
lib: new FileRef(join(__dirname, 'lib')), lib: new FileRef(join(__dirname, 'lib')),
'cache-handler.js': new FileRef(join(__dirname, 'cache-handler.js')),
'middleware.js': new FileRef(join(__dirname, 'middleware.js')), 'middleware.js': new FileRef(join(__dirname, 'middleware.js')),
'data.txt': new FileRef(join(__dirname, 'data.txt')), 'data.txt': new FileRef(join(__dirname, 'data.txt')),
'.env': new FileRef(join(__dirname, '.env')), '.env': new FileRef(join(__dirname, '.env')),
...@@ -37,6 +38,9 @@ describe('required server files app router', () => { ...@@ -37,6 +38,9 @@ describe('required server files app router', () => {
'.env.production': new FileRef(join(__dirname, '.env.production')), '.env.production': new FileRef(join(__dirname, '.env.production')),
}, },
nextConfig: { nextConfig: {
experimental: {
incrementalCacheHandlerPath: './cache-handler.js',
},
eslint: { eslint: {
ignoreDuringBuilds: true, ignoreDuringBuilds: true,
}, },
...@@ -86,6 +90,7 @@ describe('required server files app router', () => { ...@@ -86,6 +90,7 @@ describe('required server files app router', () => {
cwd: next.testDir, cwd: next.testDir,
} }
) )
appPort = `http://127.0.0.1:${appPort}`
} }
beforeAll(async () => { beforeAll(async () => {
...@@ -196,4 +201,24 @@ describe('required server files app router', () => { ...@@ -196,4 +201,24 @@ describe('required server files app router', () => {
expect(res.headers.get('x-next-cache-tags')).toBeFalsy() expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
} }
}) })
it('should not send invalid soft tags to cache handler', async () => {
for (const path of [
'/ssr/first',
'/ssr/second',
'/api/ssr/first',
'/api/ssr/second',
]) {
const res = await fetchViaHTTP(
appPort,
path,
{ hello: 'world' },
{
redirect: 'manual',
}
)
expect(res.status).toBe(200)
expect(res.headers.get('x-next-cache-tags')).toBeFalsy()
}
})
}) })
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