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

feature: Service worker (custom Astro integration) #242

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

decrek
Copy link
Member

@decrek decrek commented Jan 6, 2025

Changes

  • Add service worker setup with basic network first strategy on HTML pages
  • Made the decision to go for my own custom integration. It can serve as an example integration and I like the fact that we build our own service worker from a service-worker.ts file instead of generating one. I feel this is the most understandable for others and gives you maximum freedom over its implementation.

Associated issue

Resolves #199

How to test

  1. Open preview link
  2. Open devtools --> Application
  3. Verify a service worker is installed
  4. Go offline, refresh the page and verify that the HTML page is still served

Checklist

  • I have performed a self-review of my own code
  • I have made sure that my PR is easy to review (not too big, includes comments)
  • I have made updated relevant documentation files (in project README, docs/, etc)
  • I have added a decision log entry if the change affects the architecture or changes a significant technology
  • I have notified a reviewer

@decrek decrek requested a review from jbmoelker January 6, 2025 08:25
# Conflicts:
#	src/components/PerfHead/PerfHead.astro
Copy link

cloudflare-workers-and-pages bot commented Jan 6, 2025

Deploying head-start with  Cloudflare Pages  Cloudflare Pages

Latest commit: a061c0c
Status: ✅  Deploy successful!
Preview URL: https://5d76137e.head-start.pages.dev
Branch Preview URL: https://feat-199-add-service-worker.head-start.pages.dev

View logs

@jurgenbelien jurgenbelien changed the title Add service worker trough astro integration Add service worker through astro integration Jan 6, 2025
@jbmoelker jbmoelker changed the title Add service worker through astro integration feature: Service worker (custom Astro integration) Jan 6, 2025
@@ -0,0 +1,64 @@
import type { APIRoute, AstroIntegration } from 'astro';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would move this file to config/astro/service-worker-integration.ts or config/astro/integrations/service-worker.ts, since it's basically a configuration snippet that's loaded into the main astro.config.ts. Whereas /scripts/ contains all scripts that are executed from a cli context.

import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';

const serviceWorkerSrc = fileURLToPath(new URL('../src/assets/service-worker.ts', import.meta.url));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think within an Astro integration you have access to the configured src and dist directory (srcDir and outDir on the config param) (reference).

So I would change the API of the serviceWorkerIntegration so it can be called as something like:

serviceWorkerIntegration({
  srcFilename: 'assets/service-worker.ts', // relative to `srcDir`,
  outFilename: 'service-worker.ts',        // relative to `outDir`,
})

import { CacheableResponsePlugin } from 'workbox-cacheable-response';
import { ExpirationPlugin } from 'workbox-expiration';

((self: ServiceWorkerGlobalScope) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be simplified by declaring self?:

/// <reference lib="webworker" />
declare const self: ServiceWorkerGlobalScope;

Copy link
Contributor

@jurgenbelien jurgenbelien Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at this with @decrek and there are two ways to do it:

declare const self: ServiceWorkerGlobalScope;
export default null // or export default {}

or do the self-invoking function to assign it to a new similarly named variable.

Without the export statement, it'll throw errors. Since the service-worker is specifically not meant to be imported, the self-invoking function made more sense.

Copy link
Member

@jbmoelker jbmoelker Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I read that in fact. I would opt for declare + export default null (with a comment). Since I think the expected behaviour is that you can write self.add... in the root. So I think we should write the plumbing to make that work.

@@ -20,3 +20,17 @@ const fontFaceDeclaration = fonts.map((font) => getFontFaceDeclaration(font)).jo
))
}
<style set:html={fontFaceDeclaration}></style>

<script is:inline>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PerfHead sounds like the perfect location for this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Service Worker
3 participants