Skip to content

Commit

Permalink
fix(next-pwa): fixed __PWA_SW_ENTRY_WORKER__ being undefined (#34)
Browse files Browse the repository at this point in the history
[bump]
  • Loading branch information
DuCanhGH authored Jun 16, 2023
1 parent 9c8b3a1 commit 9850306
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/short-ducks-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ducanh2912/next-pwa": patch
---

fix(next-pwa): fixed `__PWA_SW_ENTRY_WORKER__` being undefined

- This happens when `cacheOnFrontEndNav` is not enabled, which causes `webpack.DefinePlugin` to not be called.
5 changes: 5 additions & 0 deletions packages/next-pwa/src/build-sw-entry-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ export const buildSWEntryWorker = ({
id,
destDir,
minify,
shouldGenSWEWorker,
}: {
id: string;
destDir: string;
minify: boolean;
shouldGenSWEWorker: boolean;
}) => {
if (!shouldGenSWEWorker) {
return undefined;
}
const name = `sw-entry-worker-${id}.js`;
const swEntryWorkerEntry = path.join(__dirname, `sw-entry-worker.js`);

Expand Down
22 changes: 11 additions & 11 deletions packages/next-pwa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,18 @@ const withPWAInit = (pluginOptions: PluginOptions = {}): WithPWA => {

if (!options.isServer) {
const _dest = path.join(options.dir, dest);
const sweWorkerName = buildSWEntryWorker({
id: buildId,
destDir: _dest,
minify: !dev,
shouldGenSWEWorker: cacheOnFrontEndNav,
});

if (cacheOnFrontEndNav) {
config.plugins.push(
new webpack.DefinePlugin({
__PWA_SW_ENTRY_WORKER__: `'${buildSWEntryWorker({
id: buildId,
destDir: _dest,
minify: !dev,
})}'`,
})
);
}
config.plugins.push(
new webpack.DefinePlugin({
__PWA_SW_ENTRY_WORKER__: sweWorkerName && `'${sweWorkerName}'`,
})
);

const customWorkerScriptName = buildCustomWorker({
id: buildId,
Expand Down

0 comments on commit 9850306

Please sign in to comment.