Skip to content
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

Add experiment for prefetching #9747

Merged
merged 1 commit into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_EXPORT_TRAILING_SLASH': JSON.stringify(
config.exportTrailingSlash
),
'process.env.__NEXT_PREFETCH_PRELOAD': JSON.stringify(
config.experimental.prefetchPreload
),
'process.env.__NEXT_DEFER_SCRIPTS': JSON.stringify(
config.experimental.deferScripts
),
Expand Down
10 changes: 7 additions & 3 deletions packages/next/client/page-loader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/* global document, window */
import mitt from '../next-server/lib/mitt'

const prefetchOrPreload = process.env.__NEXT_PREFETCH_PRELOAD
? 'prefetch'
: 'preload'

function supportsPreload(el) {
try {
return el.relList.supports('preload')
return el.relList.supports(prefetchOrPreload)
} catch {
return false
}
Expand All @@ -13,7 +17,7 @@ const hasPreload = supportsPreload(document.createElement('link'))

function preloadLink(url, resourceType) {
const link = document.createElement('link')
link.rel = 'preload'
link.rel = prefetchOrPreload
link.crossOrigin = process.crossOrigin
link.href = url
link.as = resourceType
Expand Down Expand Up @@ -217,7 +221,7 @@ export default class PageLoader {
// its own deduping mechanism.
if (
document.querySelector(
`link[rel="preload"][href^="${url}"], script[data-next-page="${route}"]`
`link[rel="${prefetchOrPreload}"][href^="${url}"], script[data-next-page="${route}"]`
)
) {
return
Expand Down