Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Legacy fallback env var #2074

Merged
merged 23 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ The Next.js Runtime fully supports ISR on Netlify. For more details see

Note that Netlify has a minimum TTL of 60 seconds for revalidation.

## Disable Static 404 on Dynamic Routes with fallback:false

Currently when hitting a non-prerendered path with `fallback=false` it will default to a 404 page. You can now change this default setting by using the environemnt variable `LEGACY_FALLBACK_FALSE=true`. With the environment variable set, those non-prerendered paths will now be routed through using the ISR Handler and will allow you to add redirects for those non-prerendered paths.

## Use with `next export`

If you are using `next export` to generate a static site, you do not need most of the functionality of this Next.js
Expand Down
5 changes: 5 additions & 0 deletions demos/default/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ module.exports = {
destination: '/',
permanent: true,
},
{
source: '/getStaticProps/4/',
destination: '/',
permanent: true,
},
]
},
// https://nextjs.org/docs/basic-features/image-optimization#domains
Expand Down
9 changes: 7 additions & 2 deletions packages/runtime/src/helpers/redirects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NetlifyConfig } from '@netlify/build'
import { yellowBright } from 'chalk'
import destr from 'destr'
import { readJSON } from 'fs-extra'
import type { NextConfig } from 'next'
import type { PrerenderManifest, SsgRoute } from 'next/dist/build'
Expand Down Expand Up @@ -194,7 +195,7 @@ const generateStaticIsrRewrites = ({
/**
* Generate rewrites for all dynamic routes
*/
const generateDynamicRewrites = ({
export const generateDynamicRewrites = ({
dynamicRoutes,
prerenderedDynamicRoutes,
middleware,
Expand Down Expand Up @@ -238,7 +239,11 @@ const generateDynamicRewrites = ({
withData: true,
}),
)
} else if (prerenderedDynamicRoutes[route.page].fallback === false && !is404Isr) {
} else if (
prerenderedDynamicRoutes[route.page].fallback === false &&
!is404Isr &&
!destr(process.env.LEGACY_FALLBACK_FALSE)
) {
dynamicRewrites.push(...redirectsForNext404Route({ route: route.page, buildId, basePath, i18n }))
} else {
dynamicRewrites.push(
Expand Down
113 changes: 113 additions & 0 deletions test/helpers/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
import Chance from 'chance'
import type { PrerenderManifest } from 'next/dist/build'
import { ExperimentalConfig } from 'next/dist/server/config-shared'

import { generateDynamicRewrites } from '../../packages/runtime/src/helpers/redirects'
import {
getCustomImageResponseHeaders,
getRemotePatterns,
ImagesConfig,
redirectsForNext404Route,
} from '../../packages/runtime/src/helpers/utils'

const basePrerenderManifest: PrerenderManifest = {
version: 3,
routes: {},
dynamicRoutes: {},
notFoundRoutes: [],
}

const prerenderManifest: PrerenderManifest = {
...basePrerenderManifest,
dynamicRoutes: {
'/getStaticProps/[id]': {
routeRegex: '^/getStaticProps/([^/]+?)(?:/)?$',
dataRoute: '/_next/data/build-id/getStaticProps/[id].json',
fallback: false,
dataRouteRegex: '^/_next/data/build\\-id/getStaticProps/([^/]+?)\\.json$',
},
},
}

const dynamicRoutes = [
{
page: '/getStaticProps/[id]',
regex: '^/getStaticProps/([^/]+?)(?:/)?$',
routeKeys: {
nextParamid: 'nextParamid',
},
namedRegex: '^/getStaticProps/(?<nextParamid>[^/]+?)(?:/)?$',
},
]

const route = {
dynamicRoutes,
prerenderedDynamicRoutes: prerenderManifest.dynamicRoutes,
basePath: '',
i18n: null,
buildId: 'test',
middleware: [],
is404Isr: false,
}

const chance = new Chance()

describe('getCustomImageResponseHeaders', () => {
Expand Down Expand Up @@ -132,4 +174,75 @@ describe('redirectsForNext404Route', () => {
{ force: false, from: '/fr/test', status: 404, to: '/server/pages/fr/404.html' },
])
})

it('returns static 404 redirects when LEGACY_FALLBACK_FALSE is not set', async () => {
const expected = {
dynamicRewrites: [
{
force: false,
from: '/_next/data/test/getStaticProps/:id.json',
status: 404,
to: '/server/pages/404.html',
},
{
force: false,
from: '/getStaticProps/:id',
status: 404,
to: '/server/pages/404.html',
},
],
dynamicRoutesThatMatchMiddleware: [],
}

expect(generateDynamicRewrites(route)).toStrictEqual(expected)
})

it('does not return static 404 redirects when LEGACY_FALLBACK_FALSE is true', async () => {
taty2010 marked this conversation as resolved.
Show resolved Hide resolved
process.env.LEGACY_FALLBACK_FALSE = 'true'

const expected = {
dynamicRewrites: [
{
force: false,
from: '/_next/data/test/getStaticProps/:id.json',
status: 200,
to: '/.netlify/builders/___netlify-odb-handler',
},
{
force: false,
from: '/getStaticProps/:id',
status: 200,
to: '/.netlify/builders/___netlify-odb-handler',
},
],
dynamicRoutesThatMatchMiddleware: [],
}

expect(generateDynamicRewrites(route)).toStrictEqual(expected)
})

it('returns static 404 redirects when LEGACY_FALLBACK_FALSE is set as "false"', async () => {
// testing to make sure that the any other string other than 'true' still returns the static 404 redirects
process.env.LEGACY_FALLBACK_FALSE = 'false'

const expected = {
dynamicRewrites: [
{
force: false,
from: '/_next/data/test/getStaticProps/:id.json',
status: 404,
to: '/server/pages/404.html',
},
{
force: false,
from: '/getStaticProps/:id',
status: 404,
to: '/server/pages/404.html',
},
],
dynamicRoutesThatMatchMiddleware: [],
}

expect(generateDynamicRewrites(route)).toStrictEqual(expected)
})
taty2010 marked this conversation as resolved.
Show resolved Hide resolved
})