Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cofen): fixed error 'URL object could not be cloned.' #127

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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