-
Notifications
You must be signed in to change notification settings - Fork 27k
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
fix: use a client-side navigation when redirecting to a rewriten URL #25990
Merged
kodiakhq
merged 20 commits into
vercel:canary
from
antoinechalifour:redirect-getserverside-props-with-rewrites
Jun 16, 2021
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
12fb755
fix: client-side navigation when redirecting to a rewriten URL
066846c
refactor: move gssp redirect with rewrites tests next to gssp-redirec…
84a2234
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour fa14159
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour 658b9d6
revert: component name in documentation updated while refactoring
6120fd3
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour 94c2fa8
reove check known route as there is a fallback anyway
b1e8c77
update build output size
31c46a0
Update test/integration/gssp-redirect-with-rewrites/test/index.test.js
antoinechalifour 682935b
Update test/integration/gssp-redirect-with-rewrites/test/index.test.js
antoinechalifour 9a85b56
Update test/integration/gssp-redirect-with-rewrites/test/index.test.js
antoinechalifour 9544294
fix typo
8823e06
add test for redirects to unknown route
9a862d3
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
antoinechalifour a3f6a61
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
ijjk fd722c4
update size test
ijjk efa698b
Merge remote-tracking branch 'upstream/canary' into redirect-getserve…
ijjk ab5954d
Update test
ijjk f932956
lint-fix
ijjk 1f46641
Merge branch 'canary' into redirect-getserverside-props-with-rewrites
ijjk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a similar test but instead ensure the
/unknown-route
being returned as a redirect does do a full navigation instead of a client navigationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done ! Thanks for your suggestions 😄