forked from shadowwalker/next-pwa
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fixed invalid precache manifest with
assetPrefix
[bump]
- Loading branch information
Showing
12 changed files
with
159 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@ducanh2912/next-pwa": patch | ||
--- | ||
|
||
fix(core): fixed invalid precache manifest with `assetPrefix` | ||
|
||
- Turns out it is much more complex than we thought. To make this work with `assetPrefix`, `distDir`, and `basePath`, we now remove `${publicPath}${publicDirRelativeToOutputPath}` from public files in `manifestTransforms` because `assetPrefix` is not intended for files that are in the public directory and we also want to remove `/_next/${publicDirRelativeToOutputPath}` from the URL, since that is not how we resolve files in the public directory. |
Binary file not shown.
8 changes: 8 additions & 0 deletions
8
packages/next-pwa/__tests__/integration/asset-prefix/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const RootLayout = ({ children }: { children: React.ReactNode }) => ( | ||
<html lang="en"> | ||
<head /> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
|
||
export default RootLayout; |
10 changes: 10 additions & 0 deletions
10
packages/next-pwa/__tests__/integration/asset-prefix/app/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Image from "next/image"; | ||
|
||
const Page = () => ( | ||
<main> | ||
<p id="welcome-text">This is a Next.js PWA!</p> | ||
<Image src="/next.svg" alt="Next.js Logo" width={180} height={37} priority /> | ||
</main> | ||
); | ||
|
||
export default Page; |
28 changes: 28 additions & 0 deletions
28
packages/next-pwa/__tests__/integration/asset-prefix/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createDescribe } from "../../test-utils/index.ts"; | ||
|
||
createDescribe("integration basePath", { sourceDir: __dirname, skipInstall: false }, ({ next, testMode }) => { | ||
it("should render", async () => { | ||
const $ = await next.render("/next-pwa"); | ||
expect($("#welcome-text").text()).toBe("This is a Next.js PWA!"); | ||
}); | ||
|
||
it("should fetch image", async () => { | ||
const image = await next.fetch("/next-pwa/next.svg"); | ||
expect(image.status).toBe(200); | ||
const favicon = await next.fetch("/next-pwa/favicon.ico"); | ||
expect(favicon.status).toBe(200); | ||
}); | ||
|
||
it("should be able to fetch service worker", async () => { | ||
const sw = await next.fetch("/next-pwa/sw.js"); | ||
expect(sw.status).toBe(200); | ||
expect(sw.headers.get("Content-Type")?.includes("application/javascript")).toBe(true); | ||
const swContent = await sw.text(); | ||
if (testMode === "start") { | ||
expect(/url:\"\/next-pwa\/swe-worker-(.*?).js\"/.test(swContent)).toBe(true); | ||
expect(/url:\"\/next-pwa\/_next\/..\/public\/swe-worker-(.*?).js\"/.test(swContent)).toBe(false); | ||
expect(/url:\"https:\/\/example.com\/_next\/..\/public\/swe-worker-(.*?).js\"/.test(swContent)).toBe(false); | ||
expect(/url:\"https:\/\/example.com\/_next\/static\/chunks\/main-app-(.*?).js\"/.test(swContent)).toBe(true); | ||
} | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/next-pwa/__tests__/integration/asset-prefix/next.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// @ts-check | ||
import withPWAInit from "@ducanh2912/next-pwa"; | ||
|
||
const withPWA = withPWAInit({ | ||
dest: "public", | ||
cacheOnFrontEndNav: true, | ||
}); | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
assetPrefix: "https://example.com", | ||
basePath: "/next-pwa", | ||
}; | ||
|
||
export default withPWA(nextConfig); |
1 change: 1 addition & 0 deletions
1
packages/next-pwa/__tests__/integration/asset-prefix/public/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions
29
packages/next-pwa/__tests__/integration/asset-prefix/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowImportingTsExtensions": true, | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
} | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* For a given path, this function ensures that there is no backslash | ||
* escaping slashes in the path. | ||
* | ||
* @param path | ||
*/ | ||
export const normalizePathSep = (path: string): string => { | ||
return path.replace(/\\/g, "/"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
Copyright 2018 Google LLC | ||
Use of this source code is governed by an MIT-style | ||
license that can be found in the LICENSE file or at | ||
https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
import path from "node:path"; | ||
|
||
import type { Compilation } from "webpack"; | ||
|
||
import { normalizePathSep } from "./normalize-path-sep.js"; | ||
|
||
/** | ||
* @param compilation The webpack compilation. | ||
* @param pathValue The original path value. | ||
* | ||
* @returns If path was not absolute, the returns path as-is. | ||
* Otherwise, returns path relative to the compilation's output path. | ||
* | ||
* @private | ||
*/ | ||
export function relativeToOutputPath(compilation: Compilation, pathValue: string): string { | ||
// See https://github.com/jantimon/html-webpack-plugin/pull/266/files#diff-168726dbe96b3ce427e7fedce31bb0bcR38 | ||
if (path.resolve(pathValue) === path.normalize(pathValue)) { | ||
return normalizePathSep(path.relative(compilation.options.output.path!, pathValue)); | ||
} | ||
|
||
// Otherwise, return swDest as-is. | ||
return normalizePathSep(pathValue); | ||
} |