Skip to content

Commit

Permalink
fix(cache-on-front-end-nav): fixed error 'URL object could not be clo…
Browse files Browse the repository at this point in the history
…ned.' (#127)

[bump]
  • Loading branch information
DuCanhGH authored Dec 27, 2023
1 parent 5198496 commit a4b8926
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changeset/small-dingos-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@ducanh2912/next-pwa": patch
---

fix(cache-on-front-end-nav): fixed error 'URL object could not be cloned.'

- This was due to us trying to send the URL object to our worker through `postMessage`.
4 changes: 0 additions & 4 deletions docs/content/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ type: Docs
Take a look around:

- [`next-pwa`](/docs/next-pwa/getting-started)

## What's the plan for the future?

- In the future, I plan on supporting [`Turbopack`](https://turbo.build/pack) as soon as we get our hands on them.
2 changes: 1 addition & 1 deletion docs/content/next-pwa/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ It is also really configurable as it uses Workbox under the hood to build the se
See [Getting started](/docs/next-pwa/getting-started). It should have abundant information on how to set the plugin up, including additional information for
older versions of Next.js and tips!

## And how to configure it?
## How to configure it?

See [Configuring](/docs/next-pwa/configuring). All available options are listed there!

Expand Down
4 changes: 2 additions & 2 deletions packages/next-pwa/src/sw-entry-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export type MessageType =
| {
type: "__FRONTEND_NAV_CACHE__";
shouldCacheAggressively: boolean;
url: URL | string;
url: string;
}
| {
type: "__START_URL_CACHE__";
url: URL | string;
url: string;
};

self.onmessage = async (ev: MessageEvent<MessageType>) => {
Expand Down
23 changes: 18 additions & 5 deletions packages/next-pwa/src/sw-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,31 @@ if (

if (__PWA_CACHE_ON_FRONT_END_NAV__ || __PWA_START_URL__) {
const cacheOnFrontEndNav = async (
url?: string | URL | null | undefined
originalUrl?: string | URL | null | undefined
) => {
if (!window.navigator.onLine || !url) {
if (!window.navigator.onLine) {
return;
}
const isStartUrl = !!__PWA_START_URL__ && url === __PWA_START_URL__;

const url = originalUrl
? originalUrl instanceof URL
? originalUrl.toString()
: typeof originalUrl === "string"
? originalUrl
: undefined
: undefined;

if (typeof url !== "string") return;

const isStartUrl =
!!__PWA_START_URL__ && originalUrl === __PWA_START_URL__;

if (isStartUrl) {
if (!swEntryWorker) {
const response = await fetch(url);
const response = await fetch(originalUrl);
if (!response.redirected) {
const startUrlCache = await caches.open("start-url");
return startUrlCache.put(url, response);
return startUrlCache.put(originalUrl, response);
}
} else {
swEntryWorker.postMessage({
Expand Down

0 comments on commit a4b8926

Please sign in to comment.