Replies: 2 comments 25 replies
-
Big +1 from me. Would want to talk about implementation details though. Want to avoid exposing a lot of internal code publicly that prevents us from making changes (don't want to accidentally break integrations). Can you explain what it is you want to do with Streaming seems like a separate issue to me. We are very close to having streaming work without needing any special user-code at all. Assuming that a service worker integration is creating an App instance like in SSR, that should just work once we finish off streaming. |
Beta Was this translation helpful? Give feedback.
-
Sidestepping the other thread a little bit for this discussion:
Is there anywhere I can see this in action/progress? This already sounds great, but I'd love to make sure we'd be able to do something similar to the workbox example: import { strategy } from 'workbox-streams';
import { registerRoute } from 'workbox-core';
const streamResponse = strategy([
() => caches.match(HEADER_CACHE_KEY, {cacheName: CACHE_NAME}),
() => `<nav>sidebar<ul><li><a href="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/about">about</a></li></ul></nav>`,
({event}) => apiStrategy.handle({
event: event,
request: new Request('/content/foo.md'),
}),
() => caches.match(FOOTER_CACHE_KEY, {cacheName: CACHE_NAME}),
]);
registerRoute('/foo', streamResponse); |
Beta Was this translation helpful? Give feedback.
-
This weekend I worked on a bit of a crazy PoC to see if I can run Astro in a service worker, and it turns out that you totally can:
astro-sw.mov
This is huge, because:
This lead to a bunch of other thoughts on how to improve this, and try to see how far I can take this. Currently the implementation I hacked together for this is a bit hacky and lives here thepassle/astro-service-worker. It's currently implemented as an adapter. This is not ideal, because currently having only 1 adapter in your
astro.config.mjs
is supported.Looking at Astro's source code, I realized perhaps a better fit would be an integration, that adds a vite/rollup plugin that creates a virtual mode to bundle the service worker code. I have some code put together in this gist to make this happen, but there are a few missing pieces of the puzzles, which I'll attempt to describe here.
Missing pieces/apis to make this happen
Ideally, this would be be an integration, used like so:
There are a few things missing to make this happen though. In order to create the service worker bundle, I need to have access to the SSR Manifest, which currently is not available in any of the integration hooks or vite/rollup hooks. I've created an issue for this here.
Additionally, it'd be nice to be able to import the
manifestReplace
andpagesVirtualModuleId
, but they are not available inastro/core
's package export map. I've currently copied them from Astro's source code, and hardcoded them in my code, but this is fragile, because if Astro changes them, my code will break.Ideally, I'd also have access to the pageData in my integration/vite plugin, which is in
internals
viaeachPageData(internals)
, butinternals
is not exposed anywhere either. Having access to internals also gives greater control over which code should be server-only and service-worker-only.Taking things to the next level: Streaming, offline-capable Astro apps
Then, I was reminded of this get-together by Jeff Posnick and Luke Edwards: https://cloudflare.tv/event/6ZJ5mEjrgcnCtBXBsUtyqV and Jeff's demos of streaming service workers.
Dreaming even further, it would be even more amazing to be able to stitch together stream responses in Astro components.
In a similar fashion to this Workbox example:
As Alex Russell says:
Given that the
render
function is a tagged template literal which returns anAstroComponent
, it seems like something like this should not be completely impossible in the future, but likely requires some changes in Astro.E.g.: if
expression.type === 'streamable'
, execute the callback which returns the stream, and stream it along to the browser.Beta Was this translation helpful? Give feedback.
All reactions