-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure SSG data 404 handles correctly for non-notFound (#20622)
Follow-up to #20594 this ensures non-notFound SSG data 404s do cause a hard navigation as this signals a new deployment has occurred and a hard navigation will load the new deployment's version of the page. Closes: #20623
- Loading branch information
Showing
8 changed files
with
178 additions
and
16 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const getStaticProps = () => { | ||
return { | ||
props: { | ||
hello: 'world', | ||
random: Math.random(), | ||
}, | ||
} | ||
} | ||
|
||
export default function Page(props) { | ||
return ( | ||
<> | ||
<p id="gsp">getStaticProps page</p> | ||
<p id="props">{JSON.stringify(props)}</p> | ||
</> | ||
) | ||
} |
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,17 @@ | ||
export const getServerSideProps = () => { | ||
return { | ||
props: { | ||
hello: 'world', | ||
random: Math.random(), | ||
}, | ||
} | ||
} | ||
|
||
export default function Page(props) { | ||
return ( | ||
<> | ||
<p id="gssp">getServerSideProps page</p> | ||
<p id="props">{JSON.stringify(props)}</p> | ||
</> | ||
) | ||
} |
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,3 @@ | ||
export default function Page() { | ||
return <p id="index">Index page</p> | ||
} |
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,112 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import http from 'http' | ||
import httpProxy from 'http-proxy' | ||
import webdriver from 'next-webdriver' | ||
import { | ||
findPort, | ||
killApp, | ||
launchApp, | ||
nextBuild, | ||
nextStart, | ||
} from 'next-test-utils' | ||
|
||
jest.setTimeout(1000 * 60 * 1) | ||
const appDir = join(__dirname, '..') | ||
|
||
let app | ||
let appPort | ||
let proxyServer | ||
let proxyPort | ||
let should404Data = false | ||
|
||
const runTests = () => { | ||
it('should hard navigate when a new deployment occurs', async () => { | ||
const browser = await webdriver(proxyPort, '/') | ||
|
||
await browser.eval('window.beforeNav = 1') | ||
expect(await browser.elementByCss('#index').text()).toBe('Index page') | ||
|
||
should404Data = true | ||
|
||
await browser.eval(`(function() { | ||
window.next.router.push('/gsp') | ||
})()`) | ||
await browser.waitForElementByCss('#gsp') | ||
|
||
expect(await browser.eval('window.beforeNav')).toBe(null) | ||
|
||
await browser.eval('window.beforeNav = 1') | ||
await browser.eval(`(function() { | ||
window.next.router.push('/gssp') | ||
})()`) | ||
await browser.waitForElementByCss('#gssp') | ||
|
||
expect(await browser.eval('window.beforeNav')).toBe(null) | ||
}) | ||
} | ||
|
||
describe('SSG data 404', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
|
||
const proxy = httpProxy.createProxyServer({ | ||
target: `http://localhost:${appPort}`, | ||
}) | ||
proxyPort = await findPort() | ||
|
||
proxyServer = http.createServer((req, res) => { | ||
if (should404Data && req.url.match(/\/_next\/data/)) { | ||
res.statusCode = 404 | ||
return res.end('not found') | ||
} | ||
proxy.web(req, res) | ||
}) | ||
|
||
await new Promise((resolve) => { | ||
proxyServer.listen(proxyPort, () => resolve()) | ||
}) | ||
}) | ||
afterAll(async () => { | ||
await killApp(app) | ||
proxyServer.close() | ||
}) | ||
|
||
runTests() | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await nextBuild(appDir) | ||
|
||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
|
||
const proxy = httpProxy.createProxyServer({ | ||
target: `http://localhost:${appPort}`, | ||
}) | ||
proxyPort = await findPort() | ||
|
||
proxyServer = http.createServer((req, res) => { | ||
if (should404Data && req.url.match(/\/_next\/data/)) { | ||
res.statusCode = 404 | ||
return res.end('not found') | ||
} | ||
proxy.web(req, res) | ||
}) | ||
|
||
await new Promise((resolve) => { | ||
proxyServer.listen(proxyPort, () => resolve()) | ||
}) | ||
}) | ||
afterAll(async () => { | ||
await killApp(app) | ||
proxyServer.close() | ||
}) | ||
|
||
runTests() | ||
}) | ||
}) |