Fix service worker (#2760)

This commit is contained in:
Patryk Andrzejewski 2022-12-01 11:55:46 +01:00 committed by GitHub
parent 7bcc6dac00
commit 9bd69248d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 1392 additions and 2729 deletions

4063
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -169,6 +169,7 @@
"eslint-plugin-local-rules": "^0.1.1", "eslint-plugin-local-rules": "^0.1.1",
"eslint-plugin-prefer-arrow": "^1.1.6", "eslint-plugin-prefer-arrow": "^1.1.6",
"eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.1",
"eslint-plugin-simple-import-sort": "^5.0.3", "eslint-plugin-simple-import-sort": "^5.0.3",
"file-loader": "^5.0.2", "file-loader": "^5.0.2",
"fork-ts-checker-webpack-plugin": "^3.1.1", "fork-ts-checker-webpack-plugin": "^3.1.1",
@ -196,18 +197,18 @@
"setup-polly-jest": "^0.9.1", "setup-polly-jest": "^0.9.1",
"start-server-and-test": "^1.11.0", "start-server-and-test": "^1.11.0",
"ts-jest": "^27.0.7", "ts-jest": "^27.0.7",
"tsconfig-paths-webpack-plugin": "^3.2.0",
"typescript": "^4.8.4", "typescript": "^4.8.4",
"vite": "^3.2.4",
"vite-plugin-html": "^3.2.0",
"vite-plugin-pwa": "^0.13.3",
"vite-plugin-sentry": "^1.1.6",
"vite-plugin-swc-react-refresh": "^2.2.1",
"workbox-cacheable-response": "^6.1.2", "workbox-cacheable-response": "^6.1.2",
"workbox-expiration": "^6.1.2", "workbox-expiration": "^6.1.2",
"workbox-precaching": "^6.1.2", "workbox-precaching": "^6.1.2",
"workbox-routing": "^6.1.2", "workbox-routing": "^6.1.2",
"workbox-strategies": "^6.1.2", "workbox-strategies": "^6.1.2"
"vite": "^3.2.4",
"vite-plugin-html": "^3.2.0",
"vite-plugin-sentry": "^1.1.6",
"vite-plugin-swc-react-refresh": "^2.2.1",
"eslint-plugin-react-refresh": "^0.3.1",
"tsconfig-paths-webpack-plugin": "^3.2.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"fsevents": "^1.2.9" "fsevents": "^1.2.9"

View file

@ -1,15 +1,11 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-restricted-globals */ /* eslint-disable no-restricted-globals */
const { import { CacheableResponsePlugin } from "workbox-cacheable-response/CacheableResponsePlugin";
CacheableResponsePlugin, import { ExpirationPlugin } from "workbox-expiration/ExpirationPlugin";
} = require("workbox-cacheable-response/CacheableResponsePlugin"); import { precacheAndRoute } from "workbox-precaching/precacheAndRoute";
const { ExpirationPlugin } = require("workbox-expiration/ExpirationPlugin"); import { registerRoute } from "workbox-routing/registerRoute";
const { precacheAndRoute } = require("workbox-precaching/precacheAndRoute"); import { CacheFirst } from "workbox-strategies/CacheFirst";
const { registerRoute } = require("workbox-routing/registerRoute"); import { StaleWhileRevalidate } from "workbox-strategies/StaleWhileRevalidate";
const { CacheFirst } = require("workbox-strategies/CacheFirst");
const {
StaleWhileRevalidate,
} = require("workbox-strategies/StaleWhileRevalidate");
precacheAndRoute(self.__WB_MANIFEST); precacheAndRoute(self.__WB_MANIFEST);

View file

@ -2,10 +2,12 @@
import path from "path"; import path from "path";
import { defineConfig, loadEnv } from "vite"; import { defineConfig, loadEnv } from "vite";
import { createHtmlPlugin } from "vite-plugin-html"; import { createHtmlPlugin } from "vite-plugin-html";
import { VitePWA } from "vite-plugin-pwa";
import viteSentry from "vite-plugin-sentry"; import viteSentry from "vite-plugin-sentry";
import { swcReactRefresh } from "vite-plugin-swc-react-refresh"; import { swcReactRefresh } from "vite-plugin-swc-react-refresh";
export default defineConfig(({ command, mode }) => { export default defineConfig(({ command, mode }) => {
const isDev = command !== "build";
const env = loadEnv(mode, process.cwd(), ""); const env = loadEnv(mode, process.cwd(), "");
/* /*
Using explicit env variables, there is no need to expose all of them (security). Using explicit env variables, there is no need to expose all of them (security).
@ -63,11 +65,28 @@ export default defineConfig(({ command, mode }) => {
); );
} }
if (!isDev) {
console.log("Enabling service worker...");
plugins.push(
VitePWA({
strategies: "injectManifest",
/*
Since "src" is exposed as a root,
sw.js has to be moved above, to preventing loading in a dev mode.
*/
srcDir: "../",
filename: "sw.js",
}),
);
}
/* /*
"qs" package uses 'get-intrinsic' whish refers to the global object, we need to recreate it. "qs" package uses 'get-intrinsic' whish refers to the global object, we need to recreate it.
Issue presents only on development mode. Issue presents only on development mode.
*/ */
const globals = command !== "build" ? { global: {} } : {}; const globals = isDev ? { global: {} } : {};
return { return {
root: "src", root: "src",