Skip to content

Commit

Permalink
Fix site not loading after re-build (fixes #136)
Browse files Browse the repository at this point in the history
The source of the error is that the service-worker is looking for the
front-end assets (the `.css` and `.js` files) but they're not to be
found in the browser cache (specifically in `CacheStorage`), and the
version of the files the service-worker is looking for is not to be
found on the server as well, as they've been replaced with the newer
build files. This bug was introduced when the migration from Webpack to
Vite happened, as then the public path of the build assets changed (from
`/app.[hash].js` to `/assets/index-[hash].js`, and likewise for the CSS
files); the service-worker was not updated to reflect this change.

The solution would be to fix the service-worker, and also, for the time
being, to not clear the `ui/dist` folder when re-building the front-end
so as to keep the old assets.
  • Loading branch information
previnder committed Oct 19, 2024
1 parent 003e921 commit 50cd66c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"preview": "vite preview",
"format": "prettier . --write",
"generate-yaml": "node scripts/generate-yaml.js",
"copy-sw": "node scripts/copy-sw.js"
"copy-sw": "node scripts/copy-sw.js",
"clean": "rm -rf dist/*"
},
"dependencies": {
"clsx": "^2.1.1",
Expand Down
2 changes: 1 addition & 1 deletion ui/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const shouldCache = (request) => {
const url = new URL(request.url, `${self.location.protocol}//${self.location.host}`);
const { pathname } = url;

if (pathname.startsWith('/app.') && endsWithOneOf(pathname, ['.js', '.css'])) {
if (pathname.startsWith('/assets/') && endsWithOneOf(pathname, ['.js', '.css'])) {
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions ui/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default defineConfig({
},
},
},
build: {
emptyOutDir: false,
},
define: yamlConfigDefine,
});

Expand Down

0 comments on commit 50cd66c

Please sign in to comment.