Skip to content

Commit a4b8926

Browse files
authored
fix(cache-on-front-end-nav): fixed error 'URL object could not be cloned.' (#127)
[bump]
1 parent 5198496 commit a4b8926

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

.changeset/small-dingos-give.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@ducanh2912/next-pwa": patch
3+
---
4+
5+
fix(cache-on-front-end-nav): fixed error 'URL object could not be cloned.'
6+
7+
- This was due to us trying to send the URL object to our worker through `postMessage`.

docs/content/index.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,3 @@ type: Docs
88
Take a look around:
99

1010
- [`next-pwa`](/docs/next-pwa/getting-started)
11-
12-
## What's the plan for the future?
13-
14-
- In the future, I plan on supporting [`Turbopack`](https://turbo.build/pack) as soon as we get our hands on them.

docs/content/next-pwa/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ It is also really configurable as it uses Workbox under the hood to build the se
2020
See [Getting started](/docs/next-pwa/getting-started). It should have abundant information on how to set the plugin up, including additional information for
2121
older versions of Next.js and tips!
2222

23-
## And how to configure it?
23+
## How to configure it?
2424

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

packages/next-pwa/src/sw-entry-worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export type MessageType =
44
| {
55
type: "__FRONTEND_NAV_CACHE__";
66
shouldCacheAggressively: boolean;
7-
url: URL | string;
7+
url: string;
88
}
99
| {
1010
type: "__START_URL_CACHE__";
11-
url: URL | string;
11+
url: string;
1212
};
1313

1414
self.onmessage = async (ev: MessageEvent<MessageType>) => {

packages/next-pwa/src/sw-entry.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,31 @@ if (
3737

3838
if (__PWA_CACHE_ON_FRONT_END_NAV__ || __PWA_START_URL__) {
3939
const cacheOnFrontEndNav = async (
40-
url?: string | URL | null | undefined
40+
originalUrl?: string | URL | null | undefined
4141
) => {
42-
if (!window.navigator.onLine || !url) {
42+
if (!window.navigator.onLine) {
4343
return;
4444
}
45-
const isStartUrl = !!__PWA_START_URL__ && url === __PWA_START_URL__;
45+
46+
const url = originalUrl
47+
? originalUrl instanceof URL
48+
? originalUrl.toString()
49+
: typeof originalUrl === "string"
50+
? originalUrl
51+
: undefined
52+
: undefined;
53+
54+
if (typeof url !== "string") return;
55+
56+
const isStartUrl =
57+
!!__PWA_START_URL__ && originalUrl === __PWA_START_URL__;
58+
4659
if (isStartUrl) {
4760
if (!swEntryWorker) {
48-
const response = await fetch(url);
61+
const response = await fetch(originalUrl);
4962
if (!response.redirected) {
5063
const startUrlCache = await caches.open("start-url");
51-
return startUrlCache.put(url, response);
64+
return startUrlCache.put(originalUrl, response);
5265
}
5366
} else {
5467
swEntryWorker.postMessage({

0 commit comments

Comments
 (0)