Skip to content

fix: bypass preview for static files #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/helpers/redirects.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NetlifyConfig } from '@netlify/build'
import { yellowBright } from 'chalk'
import { readJSON } from 'fs-extra'
import globby from 'globby'
import { NextConfig } from 'next'
import { PrerenderManifest } from 'next/dist/build'
import { outdent } from 'outdent'
import { join } from 'pathe'

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

export const generateRedirects = async ({
netlifyConfig,
nextConfig: { i18n, basePath, trailingSlash },
nextConfig: { i18n, basePath, trailingSlash, appDir },
}: {
netlifyConfig: NetlifyConfig
nextConfig: Pick<NextConfig, 'i18n' | 'basePath' | 'trailingSlash'>
nextConfig: Pick<NextConfig, 'i18n' | 'basePath' | 'trailingSlash' | 'appDir'>
}) => {
const { dynamicRoutes, routes: staticRoutes }: PrerenderManifest = await readJSON(
join(netlifyConfig.build.publish, 'prerender-manifest.json'),
Expand Down Expand Up @@ -100,6 +99,8 @@ export const generateRedirects = async ({
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 })
}

const publicFiles = await globby('**/*', { cwd: join(appDir, 'public') })

// This is only used in prod, so dev uses `next dev` directly
netlifyConfig.redirects.push(
// Static files are in `static`
Expand All @@ -115,13 +116,18 @@ export const generateRedirects = async ({
to: HANDLER_FUNCTION_PATH,
status: 200,
},
// Preview mode gets forced to the function, to bypess pre-rendered pages
// Preview mode gets forced to the function, to bypass pre-rendered pages, but static files need to be skipped
...publicFiles.map((file) => ({
from: `${basePath}/${file}`,
// This is a no-op, but we do it to stop it matching the following rule
to: `${basePath}/${file}`,
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
status: 200,
})),
{
from: `${basePath}/*`,
to: HANDLER_FUNCTION_PATH,
status: 200,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore The conditions type is incorrect
conditions: { Cookie: ['__prerender_bypass', '__next_preview_data'] },
force: true,
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = {

await generateRedirects({
netlifyConfig,
nextConfig: { basePath, i18n, trailingSlash },
nextConfig: { basePath, i18n, trailingSlash, appDir },
})
},

Expand Down