From 685205aaa12d418962ea7b9db893ebdf4575daab Mon Sep 17 00:00:00 2001 From: tc-imba Date: Wed, 26 Jun 2024 06:10:12 +0800 Subject: [PATCH] fix: cache problem (final attempt?) --- example/config.js | 4 ++-- gatsby-theme-academic/gatsby-browser.js | 11 ++++++----- gatsby-theme-academic/gatsby-config.mjs | 5 +++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/example/config.js b/example/config.js index c3c0224e..82202f41 100644 --- a/example/config.js +++ b/example/config.js @@ -1,5 +1,5 @@ module.exports = { - pathPrefix: '/gatsby-theme-academic', + pathPrefix: '/', siteUrl: 'https://tc-imba.github.io', title: 'John Doe', description: 'Personal Website of John Doe', @@ -8,7 +8,7 @@ module.exports = { introduction: [ 'You can write one or several paragraphs to introduce yourself here. You can find this introduction in the `config.js` file in the template.', 'In this site, almost everything supports Markdown Syntax. For example, you can add hyperlinks in the introduction such as [fxhsb](https://github.com/Reapor-Yurnero) with\n```markdown\n[fxhsb](https://github.com/Reapor-Yurnero)\n```', - 'Here we try to test whether reloading is enabled when site updated (4).', + 'Here we try to test whether reloading is enabled when site updated (19).', ], avatar: 'avatar.png', professions: [ diff --git a/gatsby-theme-academic/gatsby-browser.js b/gatsby-theme-academic/gatsby-browser.js index 35cb970f..05613dd0 100644 --- a/gatsby-theme-academic/gatsby-browser.js +++ b/gatsby-theme-academic/gatsby-browser.js @@ -4,13 +4,14 @@ import { export const wrapPageElement = _wrapPageElement; -// trigger an immediate page refresh when an update is found -export const onServiceWorkerUpdateFound = () => { - console.log('update found, reload the page'); - window.location.reload(); +export const onRouteUpdate = () => { + window.navigator.serviceWorker.register('/sw.js').then((reg) => { + reg.update(); + }); }; +// trigger an immediate page refresh when an update is found export const onServiceWorkerUpdateReady = () => { console.log('update found, reload the page'); - window.location.reload(); + window.location.reload(true); }; diff --git a/gatsby-theme-academic/gatsby-config.mjs b/gatsby-theme-academic/gatsby-config.mjs index 6a2fe6d6..e2d1753e 100644 --- a/gatsby-theme-academic/gatsby-config.mjs +++ b/gatsby-theme-academic/gatsby-config.mjs @@ -9,9 +9,11 @@ import remarkAbbr from 'remark-abbr'; import remarkExternalLinks from 'remark-external-links'; import remarkGfm from 'remark-gfm'; import remarkMath from 'remark-math'; +import moment from 'moment'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); +const buildTime = moment().format(); const config = ({ contentPath = 'content', @@ -86,6 +88,9 @@ const config = ({ options: { name: author, short_name: author, + // we must add buildTime (or anything different at each build) + // to force refresh the service worker in gatsby-plugin-offline + description: `Build at ${buildTime}`, start_url: '/', background_color: '#304CFD', theme_color: '#304CFD',