Skip to content
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wardpeet/gatsby-plugin-static-site",
"version": "0.3.0",
"version": "0.4.0-beta.1",
"description": "A plugin that disables client side routing for gatsby",
"main": "index.js",
"author": "Ward Peeters <[email protected]>",
Expand All @@ -17,7 +17,7 @@
},
"license": "MIT",
"peerDependencies": {
"gatsby": "^3.0.0"
"gatsby": "^3.0.0 || ^4.0.0"
},
"files": [
"gatsby-browser.js",
Expand Down
29 changes: 25 additions & 4 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ exports.onClientEntry = () => {
const pagePath = window.pagePath;
const location = window.location;

const parsePathComponents = pathAndQuery => {
const queryIndex = pathAndQuery.indexOf('?');
const path =
queryIndex > -1 ? pathAndQuery.substr(0, queryIndex) : pathAndQuery;
const query = queryIndex > -1 ? pathAndQuery.substr(queryIndex) : '';

return { path, query };
};

if (
pagePath &&
pagePath !== location.pathname &&
Expand All @@ -20,10 +29,16 @@ exports.onClientEntry = () => {
const originalLoadPage = loader.loadPage;

loader.loadPageSync = path => {
// with Gatsby v4, 'path' can now be a path component, or path component + query
const {
path: pathComponent,
query: queryComponent,
} = parsePathComponents(path);

let pageResources;
// if the path is the same as our current page we know it's not a prefetch
if (path === location.pathname) {
pageResources = originalLoadPageSync(pagePath);
if (pathComponent === location.pathname) {
pageResources = originalLoadPageSync(pagePath + queryComponent);
} else {
pageResources = originalLoadPageSync(path);
}
Expand All @@ -36,10 +51,16 @@ exports.onClientEntry = () => {
};

loader.loadPage = path => {
// with Gatsby v4, 'path' can now be a path component, or path component + query
const {
path: pathComponent,
query: queryComponent,
} = parsePathComponents(path);

let pageResources;
// if the path is the same as our current page we know it's not a prefetch
if (path === location.pathname) {
pageResources = originalLoadPage(pagePath);
if (pathComponent === location.pathname) {
pageResources = originalLoadPage(pagePath + queryComponent);
} else {
pageResources = originalLoadPage(path);
}
Expand Down