-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-offline): skip prefetching all resources (#16691)
Co-authored-by: GatsbyJS Bot <[email protected]> Co-authored-by: Ward Peeters <[email protected]>
- Loading branch information
1 parent
34eae00
commit e688b0c
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
packages/gatsby-plugin-offline/src/__tests__/gatsby-browser.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,44 @@ | ||
const { onServiceWorkerActive } = require(`../gatsby-browser`) | ||
|
||
it(`does not add prefetch for preconnect/prefetch/prerender`, () => { | ||
const addHeadElement = (tagName, attributes) => { | ||
const el = document.createElement(tagName) | ||
for (let key in attributes) { | ||
el.setAttribute(key, attributes[key]) | ||
} | ||
document.head.appendChild(el) | ||
return el | ||
} | ||
|
||
// Should not be prefetched | ||
addHeadElement(`link`, { rel: `preconnect`, href: `https://gatsbyjs.org` }) | ||
addHeadElement(`link`, { rel: `prefetch`, href: `https://gatsbyjs.org` }) | ||
addHeadElement(`link`, { rel: `prerender`, href: `https://gatsbyjs.org` }) | ||
addHeadElement(`script`, { src: `https://gats.by/script.js` }) | ||
addHeadElement(`style`, { "data-href": `https://gats.by/lazy.css` }) | ||
addHeadElement(`link`, { | ||
rel: `stylesheet`, | ||
href: `https://gats.by/style.css`, | ||
}) | ||
addHeadElement(`link`, { | ||
rel: `preconnect dns-prefetch`, | ||
href: `https://www.google-analytics.com`, | ||
}) | ||
|
||
onServiceWorkerActive({ | ||
getResourceURLsForPathname: () => [], | ||
serviceWorker: { active: {} }, | ||
}) | ||
|
||
// First five link elements should be the actual resources added above | ||
// The remaining link elements should be the one added by gatsby-browser as prefetch | ||
const links = [].slice.call(document.querySelectorAll(`head > link`)) | ||
|
||
// Should add prefetch for stylesheets, scripts and lazy-loaded styles | ||
expect(links[5].href).toBe(`https://gats.by/script.js`) | ||
expect(links[5].rel).toBe(`prefetch`) | ||
expect(links[6].href).toBe(`https://gats.by/lazy.css`) | ||
expect(links[6].rel).toBe(`prefetch`) | ||
expect(links[7].href).toBe(`https://gats.by/style.css`) | ||
expect(links[7].rel).toBe(`prefetch`) | ||
}) |
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