1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Close #5607 - Bug in examples/with-data-prefetch (#6093)

ctx.pathname was set to url including the query of the page to prefetch
therefore the page was cached with the wrong key (article%3Fid=1?id=1),
and that's why the cache didn't work (right key: article?id=1)
This commit is contained in:
Matthias Wilhelm 2019-01-20 13:41:49 +01:00 committed by Tim Neutkens
parent 0714300b8c
commit b0a469233b

View file

@ -11,18 +11,16 @@ export const prefetch = async href => {
const url = typeof href !== 'string' ? format(href) : href const url = typeof href !== 'string' ? format(href) : href
const { pathname } = window.location const parsedHref = resolve(window.location.pathname, url)
const parsedHref = resolve(pathname, url) const { query, pathname } = typeof href !== 'string' ? href : parse(url, true)
const { query } = typeof href !== 'string' ? href : parse(url, true)
const Component = await Router.prefetch(parsedHref) const Component = await Router.prefetch(parsedHref)
// if Component exists and has getInitialProps // if Component exists and has getInitialProps
// fetch the component props (the component should save it in cache) // fetch the component props (the component should save it in cache)
if (Component && Component.getInitialProps) { if (Component && Component.getInitialProps) {
const ctx = { pathname: href, query, isVirtualCall: true } const ctx = { pathname, query, isVirtualCall: true }
await Component.getInitialProps(ctx) await Component.getInitialProps(ctx)
} }
} }