-
Notifications
You must be signed in to change notification settings - Fork 0
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
feature: Service worker (custom Astro integration) #242
Open
decrek
wants to merge
7
commits into
main
from
feat/199-add-service-worker
base: main
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c18e1d
feat: add service worker trough astro integration
decrek a061c0c
Merge branch 'main' into feat/199-add-service-worker
decrek 4a52f29
Merge branch 'main' into feat/199-add-service-worker
decrek 86164f3
refactor: less ts magic
decrek 6013ff7
refactor: move service-worker-integration to config dir and make the …
decrek d048c36
docs: service worker info + decision
jbmoelker 4f9c35a
fix: service worker integration
jbmoelker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,67 @@ | ||
import type { APIRoute, AstroIntegration } from 'astro'; | ||
import { join } from 'node:path'; | ||
import { fileURLToPath } from 'node:url'; | ||
import * as esbuild from 'esbuild'; | ||
|
||
const filenamePath = (filename: string) => fileURLToPath(new URL(join('../../', filename), import.meta.url)); | ||
const srcFilename = filenamePath('src/assets/service-worker.ts'); | ||
const outFilename = filenamePath('dist/service-worker.js'); | ||
|
||
export default function serviceWorkerIntegration(): AstroIntegration { | ||
return { | ||
name: 'service-worker', | ||
hooks: { | ||
'astro:config:setup': async ({ | ||
command, | ||
injectRoute, | ||
addWatchFile, | ||
}) => { | ||
const isDevelopment = command === 'dev'; | ||
if (isDevelopment) { | ||
addWatchFile(srcFilename); | ||
injectRoute({ | ||
pattern: '/service-worker.js', | ||
entrypoint: import.meta.url, | ||
}); | ||
} | ||
}, | ||
'astro:build:done': async () => { | ||
try { | ||
await esbuild.build({ | ||
entryPoints: [srcFilename], | ||
outfile: outFilename, | ||
target: ['es2020'], | ||
bundle: true, | ||
minify: true, | ||
allowOverwrite: true, | ||
sourcemap: true, | ||
}); | ||
} catch (e) { | ||
console.error('Failed to build service worker'); | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
export const GET: APIRoute = async () => { | ||
const output = await esbuild.build({ | ||
entryPoints: [srcFilename], | ||
outdir: outFilename, | ||
target: ['es2020'], | ||
bundle: true, | ||
minify: false, | ||
write: false, | ||
allowOverwrite: true, | ||
sourcemap: false, | ||
}); | ||
|
||
return new Response(output.outputFiles[0].contents, { | ||
status: 200, | ||
headers: { | ||
'Content-Type': 'application/javascript', | ||
}, | ||
}); | ||
}; |
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,13 @@ | ||
# Service Worker | ||
|
||
**Implement Service Worker using our own Astro Integration with Workbox modules for maximum control.** | ||
|
||
- Date: 2025-01-11 | ||
- Alternatives Considered: existing integrations [`astrojs-service-worker`](https://github.com/tatethurston/astrojs-service-worker) and [`zastro-service-worker`](https://github.com/zachhandley/astro-service-worker) | ||
- Decision Made By: [Declan](https://github.com/decrek), [Jurgen](https://github.com/jurgenbelien), [Jasper](https://github.com/jbmoelker) | ||
|
||
## Decision | ||
|
||
Known existing Astro integrations for Service Workers are [`astrojs-service-worker`](https://github.com/tatethurston/astrojs-service-worker) and [`zastro-service-worker`](https://github.com/zachhandley/astro-service-worker). These integrations both use [`generateSW` from `workbox-build`](https://developer.chrome.com/docs/workbox/modules/workbox-build#generatesw), which only allows modifying the service worker through configuration. | ||
|
||
To give developers using Head Start more control over their service worker we've decided add our own [Astro Integration](../../config/astro/service-worker-integration.ts) which uses an actual file as a source ([`assets/service-worker.ts`](../../src/assets/service-worker.ts)) which developers can customise. By using low-level [Workbox modules](https://developer.chrome.com/docs/workbox/modules) this control is extended further. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
esbuild.build
config between dev and prod are (and should be) mostly the same (onlyminify
andsourcemap
are different). Should we move the shared config up to the root?and use that in both places to keep everything in sync?, like: