Skip to content

Commit b3a1792

Browse files
committed
Fix SW register
1 parent f899538 commit b3a1792

File tree

14 files changed

+569
-35
lines changed

14 files changed

+569
-35
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
SKIP_PREFLIGHT_CHECK=true
1+
SKIP_PREFLIGHT_CHECK=true
2+
PUBLIC_URL=https://geekr.vercel.app

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/>
1616
<meta name="apple-mobile-web-app-capable" content="yes" />
1717
<meta name="mobile-web-app-capable" content="yes" />
18-
<link rel="manifest" href="/manifest.json" />
18+
<link rel="manifest" href="/manifest.webmanifest" />
1919
<link
2020
href="https://fonts.googleapis.com/css?family=Roboto:400,500,700&display=swap"
2121
rel="stylesheet"

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"tinycolor2": "^1.6.0",
5454
"typescript": "latest",
5555
"vite": "latest",
56+
"vite-plugin-pwa": "^0.21.2",
5657
"vite-tsconfig-paths": "^4.0.5",
5758
"workbox-build": "^6.1.1"
5859
},
@@ -89,7 +90,7 @@
8990
"@testing-library/jest-dom": "^5.11.9",
9091
"@testing-library/react": "^11.2.5",
9192
"@testing-library/user-event": "^12.8.3",
92-
"@types/node": "^18.0.0",
93+
"@types/node": "^22.13.14",
9394
"@types/react": "^18.2.42",
9495
"@types/react-color": "^3.0.4",
9596
"@types/react-detect-offline": "^2.4.5",
@@ -109,5 +110,6 @@
109110
"eslint-plugin-react-hooks": "^4.6.0",
110111
"husky": "8.0.3",
111112
"prettier": "^3.0.3"
112-
}
113-
}
113+
},
114+
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
115+
}
File renamed without changes.

src/components/blocks/UpdateNotification.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const UpdateNotification = () => {
8686

8787
useEffect(() => {
8888
const receiveMessage = (event: MessageEvent<string>) => {
89-
if (event.origin === import.meta.url) {
89+
if (event.origin === import.meta.env.BASE_URL) {
9090
if (event.data === 'showUpdateNotification') {
9191
setIsShown(true)
9292
} else if (event.data === 'showUpdateSuccessNotification') {

src/pages/Post/Statistics.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ const Statistics = ({ post }: { post: Post }) => {
556556
const share = () => {
557557
const shareData = {
558558
title,
559-
url: import.meta.url + '/post/' + id,
559+
url: window.location.origin + '/post/' + id,
560560
}
561561

562562
navigator.share(shareData).catch((e) => {

src/service-worker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ registerRoute(
4646
// Return true to signal that we want to use the handler.
4747
return true
4848
},
49-
createHandlerBoundToURL(import.meta.url + '/index.html')
49+
createHandlerBoundToURL(import.meta.env.BASE_URL + '/index.html')
5050
)
5151

5252
setCacheNameDetails({
@@ -93,9 +93,7 @@ const newResponse = (
9393

9494
const cacheHeaderPlugin = [
9595
{
96-
// TODO: fix types
97-
//@ts-expect-error temporary fix
98-
cacheWillUpdate: ({ response }) =>
96+
cacheWillUpdate: ({ response }: { response: Response }) =>
9997
newResponse(response.clone(), (headers: Headers) => {
10098
headers.set('x-sw-cache', new Date().getTime().toString())
10199
return headers

src/serviceWorker.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@ const isLocalhost = Boolean(
2626
)
2727

2828
export function register(config?: Config): void {
29-
if (
30-
import.meta.env.PROD &&
31-
'serviceWorker' in navigator
32-
) {
29+
if (import.meta.env.PROD && 'serviceWorker' in navigator) {
3330
window.addEventListener('load', () => {
34-
const swUrl = `${import.meta.url}/service-worker.js`
31+
const swUrl = '/service-worker.js'
3532

3633
if (isLocalhost) {
3734
// This is running on localhost. Let's check if a service worker still exists or not.

src/serviceWorkerConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const config: Config = {
1111
registration
1212
.unregister()
1313
.then(() => {
14-
window.postMessage('showUpdateNotification', import.meta.url)
14+
window.postMessage('showUpdateNotification', import.meta.env.BASE_URL)
1515
localStorage.setItem(NEEDS_UPDATE_KEY, 'true')
1616
console.log(
1717
'%c[info] %cSent showUpdateNotification message from SW',
@@ -29,7 +29,7 @@ const config: Config = {
2929
)
3030
},
3131
onSuccess: () => {
32-
window.postMessage('showUpdateSuccessNotification', import.meta.url)
32+
window.postMessage('showUpdateSuccessNotification', window.location.origin)
3333
console.log(
3434
'%c[success] %cService worker succeed in registration.',
3535
'color: #00e676;',

src/utils/formatLdJsonSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export default (post: Post) => {
1515

1616
if (!schema.mainEntityOfPage) return
1717

18-
schema.mainEntityOfPage['@id'] = import.meta.url + getPostLink(post)
19-
schema.url = import.meta.url + getPostLink(post)
18+
schema.mainEntityOfPage['@id'] = window.location.origin + getPostLink(post)
19+
schema.url = window.location.origin + getPostLink(post)
2020
schema.image = schema.image?.filter(
2121
(e) => !e.startsWith('https://habr.com/share/publication')
2222
)

0 commit comments

Comments
 (0)