docs: update service-worker example to be type-safe when in ts + handle offline better #10617
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.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
When introducing a service-worker into my SvelteKit application, I encountered some issues after copying the code from the docs at https://kit.svelte.dev/docs/service-workers#inside-the-service-worker.
respondfunction could returnPromise<Response | undefined>instead ofPromise<Response>, which conflicts with the argument type ofevent.respondWith. See: https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent/respondWith#syntax. The example is updated so that if thecache.matchreturnsundefined, then it does not return thatundefinedvalue, and instead continues on to try and make the fetch. In the case there is some error thrown byfetch, then it will only return the response fromcache.matchif it is defined - otherwise, it will just re-throw the error.fetchfunction did not throw - instead, it returned an NS_OK: object... this seems more like browser internals, and isn't well-documented, but passing this object back toevent.respondWithtriggered a bunch of errors, instead of hitting the cache logic. To handle this case, the response fromfetchis only returned if it's an instanceofRequest. Otherwise, it throws an error, and goes to thecatchblock.If necessary, I can provide a sample project with the previous service-worker and this service-worker to show that the existing sample does not actually work in an offline context, and with the fixes provided in this PR, it does provide the offline caching behaviour as described. Hopefully the changes make sense in isolation though, and I have tested them extensively on my own application.
For reference, this is the
service-worker.tsfile I have in my own application which does work - this was the basis for the documentation changes in this PR:Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.