Skip to content

Commit ecb3cc8

Browse files
fix: bypass preview for static files (#918)
Release-As: 4.0.0-rc.1 Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 85b6477 commit ecb3cc8

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/helpers/redirects.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { NetlifyConfig } from '@netlify/build'
2-
import { yellowBright } from 'chalk'
32
import { readJSON } from 'fs-extra'
3+
import globby from 'globby'
44
import { NextConfig } from 'next'
55
import { PrerenderManifest } from 'next/dist/build'
6-
import { outdent } from 'outdent'
76
import { join } from 'pathe'
87

98
import { HANDLER_FUNCTION_PATH, HIDDEN_PATHS, ODB_FUNCTION_PATH } from '../constants'
@@ -45,10 +44,10 @@ const generateLocaleRedirects = ({
4544

4645
export const generateRedirects = async ({
4746
netlifyConfig,
48-
nextConfig: { i18n, basePath, trailingSlash },
47+
nextConfig: { i18n, basePath, trailingSlash, appDir },
4948
}: {
5049
netlifyConfig: NetlifyConfig
51-
nextConfig: Pick<NextConfig, 'i18n' | 'basePath' | 'trailingSlash'>
50+
nextConfig: Pick<NextConfig, 'i18n' | 'basePath' | 'trailingSlash' | 'appDir'>
5251
}) => {
5352
const { dynamicRoutes, routes: staticRoutes }: PrerenderManifest = await readJSON(
5453
join(netlifyConfig.build.publish, 'prerender-manifest.json'),
@@ -100,6 +99,8 @@ export const generateRedirects = async ({
10099
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 })
101100
}
102101

102+
const publicFiles = await globby('**/*', { cwd: join(appDir, 'public') })
103+
103104
// This is only used in prod, so dev uses `next dev` directly
104105
netlifyConfig.redirects.push(
105106
// Static files are in `static`
@@ -115,13 +116,18 @@ export const generateRedirects = async ({
115116
to: HANDLER_FUNCTION_PATH,
116117
status: 200,
117118
},
118-
// Preview mode gets forced to the function, to bypess pre-rendered pages
119+
// Preview mode gets forced to the function, to bypass pre-rendered pages, but static files need to be skipped
120+
...publicFiles.map((file) => ({
121+
from: `${basePath}/${file}`,
122+
// This is a no-op, but we do it to stop it matching the following rule
123+
to: `${basePath}/${file}`,
124+
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
125+
status: 200,
126+
})),
119127
{
120128
from: `${basePath}/*`,
121129
to: HANDLER_FUNCTION_PATH,
122130
status: 200,
123-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
124-
// @ts-ignore The conditions type is incorrect
125131
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
126132
force: true,
127133
},

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ module.exports = {
7878

7979
await generateRedirects({
8080
netlifyConfig,
81-
nextConfig: { basePath, i18n, trailingSlash },
81+
nextConfig: { basePath, i18n, trailingSlash, appDir },
8282
})
8383
},
8484

0 commit comments

Comments
 (0)