From 62dd35ee5b14a07fec428d28f5f5a119efddb483 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Thu, 24 Mar 2022 15:14:12 -0400 Subject: [PATCH] chore: add support for Gatsby v4 --- package.json | 4 ++-- src/gatsby-browser.js | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 201c44f..c36c659 100644 --- a/package.json +++ b/package.json @@ -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 ", @@ -17,7 +17,7 @@ }, "license": "MIT", "peerDependencies": { - "gatsby": "^3.0.0" + "gatsby": "^3.0.0 || ^4.0.0" }, "files": [ "gatsby-browser.js", diff --git a/src/gatsby-browser.js b/src/gatsby-browser.js index 5b394eb..0193c71 100644 --- a/src/gatsby-browser.js +++ b/src/gatsby-browser.js @@ -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 && @@ -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); } @@ -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); }