Unverified Commit bc2282fa authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Speed up SSG prefetching (#18813)

This pull request speeds up Next.js' rendering pipeline by fetching data, parsing it, and loading it into memory instead of only doing the network request.

This will mainly result in improved Firefox/Safari performance since they handled prefetch incorrectly—only Chrome did it right. This also gets us closer to being able to use `no-store` in our caching headers!

---

Fixes #18639
x-ref #18802
parent 5d80e684
...@@ -217,25 +217,12 @@ export default class PageLoader { ...@@ -217,25 +217,12 @@ export default class PageLoader {
/** /**
* @param {string} href the route href (file-system path) * @param {string} href the route href (file-system path)
* @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes
*/ */
prefetchData(href: string, asPath: string, locale?: string | false) { _isSsg(href: string): Promise<boolean> {
const { pathname: hrefPathname } = parseRelativeUrl(href) const { pathname: hrefPathname } = parseRelativeUrl(href)
const route = normalizeRoute(hrefPathname) const route = normalizeRoute(hrefPathname)
return this.promisedSsgManifest!.then( return this.promisedSsgManifest!.then((s: ClientSsgManifest) =>
(s: ClientSsgManifest, _dataHref?: string) => s.has(route)
// Check if the route requires a data file
s.has(route) &&
// Try to generate data href, noop when falsy
(_dataHref = this.getDataHref(href, asPath, true, locale)) &&
// noop when data has already been prefetched (dedupe)
!document.querySelector(
`link[rel="${relPrefetch}"][href^="${_dataHref}"]`
) &&
// Inject the `<link rel=prefetch>` tag for above computed `href`.
appendLink(_dataHref, relPrefetch, 'fetch').catch(() => {
/* ignore prefetch error */
})
) )
} }
......
...@@ -1218,12 +1218,20 @@ export default class Router implements BaseRouter { ...@@ -1218,12 +1218,20 @@ export default class Router implements BaseRouter {
const route = removePathTrailingSlash(pathname) const route = removePathTrailingSlash(pathname)
await Promise.all([ await Promise.all([
this.pageLoader.prefetchData( this.pageLoader._isSsg(url).then((isSsg: boolean) => {
url, return isSsg
asPath, ? this._getStaticData(
typeof options.locale !== 'undefined' ? options.locale : this.locale, this.pageLoader.getDataHref(
this.defaultLocale url,
), asPath,
true,
typeof options.locale !== 'undefined'
? options.locale
: this.locale
)
)
: false
}),
this.pageLoader[options.priority ? 'loadPage' : 'prefetch'](route), this.pageLoader[options.priority ? 'loadPage' : 'prefetch'](route),
]) ])
} }
......
/* eslint-env jest */ /* eslint-env jest */
import webdriver from 'next-webdriver' import assert from 'assert'
import { join, resolve } from 'path' import cheerio from 'cheerio'
import url from 'url' import fs, {
existsSync,
readFileSync,
renameSync,
writeFileSync,
} from 'fs-extra'
import { import {
launchApp,
findPort,
killApp,
nextBuild,
waitFor,
check, check,
getBrowserBodyText, fetchViaHTTP,
renderViaHTTP,
File, File,
nextStart, findPort,
initNextServerScript, getBrowserBodyText,
getRedboxSource, getRedboxSource,
hasRedbox, hasRedbox,
fetchViaHTTP, initNextServerScript,
killApp,
launchApp,
nextBuild,
nextStart,
renderViaHTTP,
startStaticServer, startStaticServer,
waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import fs, { import webdriver from 'next-webdriver'
readFileSync, import { join, resolve } from 'path'
writeFileSync, import url from 'url'
renameSync,
existsSync,
} from 'fs-extra'
import cheerio from 'cheerio'
jest.setTimeout(1000 * 60 * 2) jest.setTimeout(1000 * 60 * 2)
...@@ -427,28 +428,35 @@ const runTests = (dev = false) => { ...@@ -427,28 +428,35 @@ const runTests = (dev = false) => {
const browser = await webdriver(appPort, `${basePath}/hello`) const browser = await webdriver(appPort, `${basePath}/hello`)
await browser.eval('window.next.router.prefetch("/gssp")') await browser.eval('window.next.router.prefetch("/gssp")')
await check( await check(async () => {
async () => { const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
const links = await browser.elementsByCss('link[rel=prefetch]') hrefs.sort()
let found = new Set()
assert.deepEqual(
for (const link of links) { hrefs.map((href) =>
const href = await link.getAttribute('href') new URL(href).pathname.replace(/\/_next\/data\/[^/]+/, '')
if (href.match(/(gsp|gssp|other-page)-.*?\.js$/)) { ),
found.add(href) [
} `${basePath}/gsp.json`,
if (href.match(/gsp\.json$/)) { `${basePath}/index.json`,
found.add(href) `${basePath}/index/index.json`,
} ]
} )
return found
}, const prefetches = await browser.eval(
{ `[].slice.call(document.querySelectorAll("link[rel=prefetch]")).map((e) => new URL(e.href).pathname)`
test(result) { )
return result.size === 4 expect(prefetches).toContainEqual(
}, expect.stringMatching(/\/gsp-[^./]+\.js/)
} )
) expect(prefetches).toContainEqual(
expect.stringMatching(/\/gssp-[^./]+\.js/)
)
expect(prefetches).toContainEqual(
expect.stringMatching(/\/other-page-[^./]+\.js/)
)
return 'yes'
}, 'yes')
}) })
} }
...@@ -602,23 +610,18 @@ const runTests = (dev = false) => { ...@@ -602,23 +610,18 @@ const runTests = (dev = false) => {
expect(await browser.elementByCss('#pathname').text()).toBe('/') expect(await browser.elementByCss('#pathname').text()).toBe('/')
if (!dev) { if (!dev) {
const prefetches = await browser.elementsByCss('link[rel="prefetch"]') const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
let found = false hrefs.sort()
for (const prefetch of prefetches) { expect(
const fullHref = await prefetch.getAttribute('href') hrefs.map((href) =>
const href = url.parse(fullHref).pathname new URL(href).pathname.replace(/\/_next\/data\/[^/]+/, '')
)
if ( ).toEqual([
href.startsWith(`${basePath}/_next/data`) && `${basePath}/gsp.json`,
href.endsWith('index.json') && `${basePath}/index.json`,
!href.endsWith('index/index.json') `${basePath}/index/index.json`,
) { ])
found = true
}
}
expect(found).toBe(true)
} }
}) })
...@@ -636,22 +639,18 @@ const runTests = (dev = false) => { ...@@ -636,22 +639,18 @@ const runTests = (dev = false) => {
expect(await browser.elementByCss('#pathname').text()).toBe('/index') expect(await browser.elementByCss('#pathname').text()).toBe('/index')
if (!dev) { if (!dev) {
const prefetches = await browser.elementsByCss('link[rel="prefetch"]') const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
let found = false hrefs.sort()
for (const prefetch of prefetches) { expect(
const fullHref = await prefetch.getAttribute('href') hrefs.map((href) =>
const href = url.parse(fullHref).pathname new URL(href).pathname.replace(/\/_next\/data\/[^/]+/, '')
)
if ( ).toEqual([
href.startsWith(`${basePath}/_next/data`) && `${basePath}/gsp.json`,
href.endsWith('index/index.json') `${basePath}/index.json`,
) { `${basePath}/index/index.json`,
found = true ])
}
}
expect(found).toBe(true)
} }
}) })
......
...@@ -7,6 +7,7 @@ import cheerio from 'cheerio' ...@@ -7,6 +7,7 @@ import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import escapeRegex from 'escape-string-regexp' import escapeRegex from 'escape-string-regexp'
import assert from 'assert'
import { import {
fetchViaHTTP, fetchViaHTTP,
findPort, findPort,
...@@ -312,23 +313,20 @@ function runTests(isDev) { ...@@ -312,23 +313,20 @@ function runTests(isDev) {
})()`) })()`)
await check(async () => { await check(async () => {
for (const dataPath of [ const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
'/fr/gsp.json', hrefs.sort()
'/fr/gsp/fallback/first.json',
'/fr/gsp/fallback/hello.json', assert.deepEqual(
]) { hrefs.map((href) =>
const found = await browser.eval(`(function() { new URL(href).pathname.replace(/^\/_next\/data\/[^/]+/, '')
const links = [].slice.call(document.querySelectorAll('link')) ),
[
for (var i = 0; i < links.length; i++) { '/fr/gsp.json',
if (links[i].href.indexOf("${dataPath}") > -1) { '/fr/gsp/fallback/first.json',
return true '/fr/gsp/fallback/hello.json',
} ]
} )
return false return 'yes'
})()`)
return found ? 'yes' : 'no'
}
}, 'yes') }, 'yes')
} }
...@@ -518,23 +516,20 @@ function runTests(isDev) { ...@@ -518,23 +516,20 @@ function runTests(isDev) {
})()`) })()`)
await check(async () => { await check(async () => {
for (const dataPath of [ const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
'/fr/gsp.json', hrefs.sort()
'/fr/gsp/fallback/first.json',
'/fr/gsp/fallback/hello.json', assert.deepEqual(
]) { hrefs.map((href) =>
const found = await browser.eval(`(function() { new URL(href).pathname.replace(/^\/_next\/data\/[^/]+/, '')
const links = [].slice.call(document.querySelectorAll('link')) ),
[
for (var i = 0; i < links.length; i++) { '/fr/gsp.json',
if (links[i].href.indexOf("${dataPath}") > -1) { '/fr/gsp/fallback/first.json',
return true '/fr/gsp/fallback/hello.json',
} ]
} )
return false return 'yes'
})()`)
return found ? 'yes' : 'no'
}
}, 'yes') }, 'yes')
} }
......
...@@ -10,7 +10,6 @@ import { ...@@ -10,7 +10,6 @@ import {
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { join } from 'path' import { join } from 'path'
import { readFile } from 'fs-extra' import { readFile } from 'fs-extra'
import { parse } from 'url'
jest.setTimeout(1000 * 60 * 5) jest.setTimeout(1000 * 60 * 5)
...@@ -270,54 +269,38 @@ describe('Prefetching Links in viewport', () => { ...@@ -270,54 +269,38 @@ describe('Prefetching Links in viewport', () => {
const browser = await webdriver(appPort, '/ssg/fixture') const browser = await webdriver(appPort, '/ssg/fixture')
await waitFor(2 * 1000) // wait for prefetching to occur await waitFor(2 * 1000) // wait for prefetching to occur
const links = await browser.elementsByCss('link[rel=prefetch][as=fetch]') const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
const hrefs = []
for (const link of links) {
const href = await link.getAttribute('href')
hrefs.push(href)
}
hrefs.sort() hrefs.sort()
expect(hrefs.map((href) => parse(href).pathname)).toMatchInlineSnapshot(` expect(hrefs.map((href) => new URL(href).pathname)).toEqual([
Array [ '/_next/data/test-build/ssg/basic.json',
"/_next/data/test-build/ssg/basic.json", '/_next/data/test-build/ssg/catch-all/foo.json',
"/_next/data/test-build/ssg/catch-all/foo.json", '/_next/data/test-build/ssg/catch-all/foo/bar.json',
"/_next/data/test-build/ssg/catch-all/foo/bar.json", '/_next/data/test-build/ssg/catch-all/one.json',
"/_next/data/test-build/ssg/catch-all/one.json", '/_next/data/test-build/ssg/catch-all/one/two.json',
"/_next/data/test-build/ssg/catch-all/one/two.json", '/_next/data/test-build/ssg/dynamic-nested/foo/bar.json',
"/_next/data/test-build/ssg/dynamic-nested/foo/bar.json", '/_next/data/test-build/ssg/dynamic-nested/one/two.json',
"/_next/data/test-build/ssg/dynamic-nested/one/two.json", '/_next/data/test-build/ssg/dynamic/one.json',
"/_next/data/test-build/ssg/dynamic/one.json", '/_next/data/test-build/ssg/dynamic/two.json',
"/_next/data/test-build/ssg/dynamic/two.json", ])
]
`)
}) })
it('should prefetch data files when mismatched', async () => { it('should prefetch data files when mismatched', async () => {
const browser = await webdriver(appPort, '/ssg/fixture/mismatch') const browser = await webdriver(appPort, '/ssg/fixture/mismatch')
await waitFor(2 * 1000) // wait for prefetching to occur await waitFor(2 * 1000) // wait for prefetching to occur
const links = await browser.elementsByCss('link[rel=prefetch][as=fetch]') const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
const hrefs = []
for (const link of links) {
const href = await link.getAttribute('href')
hrefs.push(href)
}
hrefs.sort() hrefs.sort()
expect(hrefs.map((href) => parse(href).pathname)).toMatchInlineSnapshot(` expect(hrefs.map((href) => new URL(href).pathname)).toEqual([
Array [ '/_next/data/test-build/ssg/catch-all/foo.json',
"/_next/data/test-build/ssg/catch-all/foo.json", '/_next/data/test-build/ssg/catch-all/foo/bar.json',
"/_next/data/test-build/ssg/catch-all/foo/bar.json", '/_next/data/test-build/ssg/catch-all/one.json',
"/_next/data/test-build/ssg/catch-all/one.json", '/_next/data/test-build/ssg/catch-all/one/two.json',
"/_next/data/test-build/ssg/catch-all/one/two.json", '/_next/data/test-build/ssg/dynamic-nested/foo/bar.json',
"/_next/data/test-build/ssg/dynamic-nested/foo/bar.json", '/_next/data/test-build/ssg/dynamic-nested/one/two.json',
"/_next/data/test-build/ssg/dynamic-nested/one/two.json", '/_next/data/test-build/ssg/dynamic/one.json',
"/_next/data/test-build/ssg/dynamic/one.json", '/_next/data/test-build/ssg/dynamic/two.json',
"/_next/data/test-build/ssg/dynamic/two.json", ])
]
`)
}) })
}) })
...@@ -25,7 +25,6 @@ import { ...@@ -25,7 +25,6 @@ import {
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { dirname, join } from 'path' import { dirname, join } from 'path'
import url from 'url'
jest.setTimeout(1000 * 60 * 2) jest.setTimeout(1000 * 60 * 2)
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
...@@ -829,28 +828,18 @@ const runTests = (dev = false, isEmulatedServerless = false) => { ...@@ -829,28 +828,18 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
document.querySelector('#to-rewritten-ssg').scrollIntoView() document.querySelector('#to-rewritten-ssg').scrollIntoView()
) )
await check( await check(async () => {
async () => { const hrefs = await browser.eval(
const links = await browser.elementsByCss('link[rel=prefetch]') `Object.keys(window.next.router.sdc)`
let found = false )
hrefs.sort()
for (const link of links) { expect(
const href = await link.getAttribute('href') hrefs.map((href) =>
const { pathname } = url.parse(href) new URL(href).pathname.replace(/^\/_next\/data\/[^/]+/, '')
)
if (pathname.endsWith('/lang/en/about.json')) { ).toContainEqual('/lang/en/about.json')
found = true return 'yes'
break }, 'yes')
}
}
return found
},
{
test(result) {
return result === true
},
}
)
} }
await browser.eval('window.beforeNav = "hi"') await browser.eval('window.beforeNav = "hi"')
await browser.elementByCss('#to-rewritten-ssg').click() await browser.elementByCss('#to-rewritten-ssg').click()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment