-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use a client-side navigation when redirecting to a rewriten URL (#…
…25990) This PR is an attempt to fix #25943. The implementation is only a proposal and I'll be happy to take any feedback about it :) Fixes: #25943 ## Bug - [X] Related issues linked using `fixes #number` - [x] Integration tests added ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. ## Documentation / Examples - [ ] Make sure the linting passes
- Loading branch information
1 parent
8610ba4
commit 70dffd4
Showing
7 changed files
with
132 additions
and
11 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
10 changes: 10 additions & 0 deletions
10
test/integration/gssp-redirect-with-rewrites/next.config.js
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,10 @@ | ||
module.exports = { | ||
async rewrites() { | ||
return [ | ||
{ | ||
source: '/alias-to-main-content', | ||
destination: '/main-content', | ||
}, | ||
] | ||
}, | ||
} |
35 changes: 35 additions & 0 deletions
35
test/integration/gssp-redirect-with-rewrites/pages/main-content.js
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,35 @@ | ||
import Link from 'next/link' | ||
|
||
export default function MainContent({ message }) { | ||
return ( | ||
<main> | ||
<h1>Hello {message}</h1> | ||
|
||
<ul> | ||
<li> | ||
<Link href="/redirector?redirect=/alias-to-main-content&message=refreshed"> | ||
<a id="link-with-rewritten-url" className={message}> | ||
Link with rewritten target url | ||
</a> | ||
</Link> | ||
</li> | ||
|
||
<li> | ||
<Link href="/redirector?redirect=/main-content&message=refreshWithClientSideNavigation"> | ||
<a>Link with client side navigation</a> | ||
</Link> | ||
</li> | ||
|
||
<li> | ||
<Link href="/redirector?redirect=/unknown-route"> | ||
<a id="link-unknown-url">Link to unknown internal navigation</a> | ||
</Link> | ||
</li> | ||
</ul> | ||
</main> | ||
) | ||
} | ||
|
||
export const getServerSideProps = ({ query }) => ({ | ||
props: { message: query.message || 'World ' }, | ||
}) |
16 changes: 16 additions & 0 deletions
16
test/integration/gssp-redirect-with-rewrites/pages/redirector.js
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,16 @@ | ||
export default function Redirector() { | ||
return ( | ||
<main> | ||
<h1>Hello world</h1> | ||
</main> | ||
) | ||
} | ||
|
||
export const getServerSideProps = ({ query }) => { | ||
return { | ||
redirect: { | ||
destination: `${query.redirect}?message=${query.message}`, | ||
permanent: false, | ||
}, | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
test/integration/gssp-redirect-with-rewrites/test/index.test.js
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,62 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { | ||
renderViaHTTP, | ||
findPort, | ||
launchApp, | ||
killApp, | ||
check, | ||
} from 'next-test-utils' | ||
import webdriver from 'next-webdriver' | ||
|
||
// test suites | ||
|
||
const context = {} | ||
jest.setTimeout(1000 * 60 * 5) | ||
|
||
describe('getServerSideProps redirects', () => { | ||
beforeAll(async () => { | ||
context.appPort = await findPort() | ||
context.server = await launchApp(join(__dirname, '../'), context.appPort, { | ||
env: { __NEXT_TEST_WITH_DEVTOOL: 1 }, | ||
}) | ||
|
||
// pre-build all pages at the start | ||
await Promise.all([ | ||
renderViaHTTP(context.appPort, '/alias-to-main-content'), | ||
renderViaHTTP(context.appPort, '/main-content'), | ||
]) | ||
}) | ||
afterAll(() => killApp(context.server)) | ||
|
||
it('should use a client-side navigation for a rewritten URL', async () => { | ||
const browser = await webdriver(context.appPort, '/alias-to-main-content') | ||
|
||
// During a browser navigation global variables are reset, | ||
// So by checking that the __SAME_PAGE variable is still defined | ||
// then the client-side navigation has happened | ||
await browser.eval('window.__SAME_PAGE = true') | ||
|
||
await browser.elementByCss('#link-with-rewritten-url').click() | ||
|
||
// Wait until the new props are rendered | ||
await browser.waitForElementByCss('.refreshed') | ||
|
||
expect(await browser.eval('window.__SAME_PAGE')).toBe(true) | ||
}) | ||
|
||
it('should fallback to browser navigation for an unknown URL', async () => { | ||
const browser = await webdriver(context.appPort, '/alias-to-main-content') | ||
|
||
// then the client-side navigation has happened | ||
await browser.eval('window.__SAME_PAGE = true') | ||
await browser.elementByCss('#link-unknown-url').click() | ||
|
||
// Wait until the page has be reloaded | ||
await check(async () => { | ||
const val = await browser.eval('window.__SAME_PAGE') | ||
return val ? 'fail' : 'success' | ||
}, 'success') | ||
}) | ||
}) |