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

convert TS examples (deno, node, sw) to use the new hello-world.html format #488

Closed

Conversation

nickchomey
Copy link

@nickchomey nickchomey commented Jan 15, 2025

This adds an example for the typescript sdk that has isomorphic routing and rendering for deno and a service worker. If you go offline, everything will be served from the SW.

  • Uses Hono for the routing etc...
  • Deno tasks watch for changes and use esbuild to rebuild the service worker as-needed. And optionally minify it.

There's a few files that are probably extraneous - caddyfile, mise.toml. Not sure what to do about those since I'd like to have them in my own repo

delaneyj and others added 30 commits December 20, 2024 08:59
* Add `javascript_api` [deploy-site]

* Add note about JSON [deploy-site]

* Mark SDKs done

* Move conditional statement (starfederation#389)

* Add release note

* Add troubleshooting tips [deploy-site]

* Replace http.Flusher with http.ResponseController for SSE in Go SDK  (starfederation#392)

* refactor: replace http.Flusher with http.ResponseController

* refactor: replace http.Flusher with http.ResponseController reintroduced panic justification

---------

Co-authored-by: drahtzieher <[email protected]>

* Improve SDKs in docs [deploy-site]

* Improve `retry` option descriptions [deploy-site]

* Wording improvements [deploy-site]

* Improve wording [deploy-site]

* Fix & unify CDN in docs/examples (starfederation#394)

* Try Biome
Fixes starfederation#395

* format all

* more

* fix ignores

* Add comment to bundles (starfederation#401)

* Add comment to bundles

* One-liner

* Build

* Add `retryInterval` option to `sse()` action (starfederation#393)

* Add `retryInterval` option to `sse()` action

* Add to `sse` plugin too

* Build

* Add release note

* Add `Datastar's philosophy` [deploy-site]

* Use `path/to/datastar.js` [deploy-site]

* Fix local signals (starfederation#407)

* Fix bundler not exporting module (starfederation#406)

* Add Banner and Format

* Export Datastar in bundler

* Release note

* Fix export

* Add ref usage example [deploy-site]

* updatego deps

* Bump org.eclipse.jetty:jetty-server in /examples/java (starfederation#408)

Bumps org.eclipse.jetty:jetty-server from 11.0.15 to 11.0.24.

---
updated-dependencies:
- dependency-name: org.eclipse.jetty:jetty-server
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Add example to error page [deploy-site]

* fix const usage in Go

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Ben Croker <[email protected]>
Co-authored-by: Ben Croker <[email protected]>
Co-authored-by: drahtzieher <[email protected]>
Co-authored-by: drahtzieher <[email protected]>
Co-authored-by: David Linke <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@nickchomey
Copy link
Author

Ive put it back in draft - suddenly it isn't working from the service worker for me... Perhaps something in one of the dependencies changed...

@nickchomey
Copy link
Author

Im just dumb - evidently I didnt properly test the most recent changes that I had made (adding minification of template literals) and it was removing /n which apparently messes with D* typescript sdk.

@Superpat I know we had talked about minification of html when we were discussing compression. But given that compression is so effective and easy, and that minification of template literals really just amounts to removing line breaks and, perhaps, some comments which dont contribute much to filesize, I don't think its at all worth fixing the sdk to support doing this. Just compress js files and sse responses.

As mentioned, Deno will hopefully compress event-steam soon. Here's the PR for it denoland/deno#27776

And its dead-simple with caddy and other reverse proxies.

@nickchomey nickchomey marked this pull request as ready for review January 22, 2025 18:32
@Superpat
Copy link
Contributor

Superpat commented Jan 26, 2025

@nickchomey I was able to test this today and while the online deno example works fine (and is very good). If I set the network state to offline in my devtools the service worker doesnt work.

I've tried in both firefox and chromium. Do I need to do anything else then deno -A deno.t

@nickchomey
Copy link
Author

nickchomey commented Jan 26, 2025

Ah, use deno run dev or one of the other tasks in deno.json. It'll build the service worker, which I assume has not been done. I suppose I could include that in the repo, pre built...

Not sure if you have to run deno install first either.

I should probably add a basic readme...

Though, I was thinking last night that it really should be a simpler demo... I'll review it all later and submit a new commit


I've also discovered some gotchas with service workers - in particular, you cant do dynamic imports of scripts with import(). Not an issue for this example, but it creates some limitations/complications for making a more comprehensive SW-rendered app (unless you include all scripts in a single bundled sw). I've found a good workaround now.

I definitely won't be including anything like that here, but it really ought to be shared in some way

Perhaps it would be best to just carry on, and when I'm ready to share an actual case study, I can link to a more comprehensive repo/guide/framework

@Superpat
Copy link
Contributor

Superpat commented Jan 27, 2025

I still cant get it to run, I ran deno run --unstable-sloppy-imports -A deno and in the console I see

Service Worker registration failed: TypeError: Failed to register a ServiceWorker for scope ('http://localhost:8000/') with script ('http://localhost:8000/service-worker.js'): A bad HTTP response code (404) was received when fetching the script.

Besides that, I think the demo should be a bit simpler yes, no need for the comparison between streamed / buffered (it's very interesting, but it would be more at home on a blog post or a page on the data-star website, maybe contribute it to the docs pr @bencroker is leading). Ideally it would be the same example as the deno / node examples. Though we could complexify those a little if you want so it's easier to showcase the offline behaviour. I imagine an input that takes a string, a button that says send and an output that is populated by sse with hello $string.

Also a readme would be crucial.

@Superpat
Copy link
Contributor

I will work on updating the other examples to the basic example I just detailed.

@nickchomey
Copy link
Author

nickchomey commented Jan 27, 2025

Again, you're not building the sw - please look at deno.json and run whichever task you prefer. I use deno run dev

Good idea making it just show the same examples as non-service worker. I'll try to do that today.

And, yes, I said I'd made a readme.

@Superpat
Copy link
Contributor

Ah sorry, I thought I ran deno run dev but I ran deno run deno by accident.

So it's telling me that esbuild does not support OpenBSD (which is strange since I can run the go version of esbuild just fine). Would you be willing to use deno dnt to build the service worker instead ? You can look at sdk/typescript/build.ts for an example. I'd prefer to keep all the ts stuff buildable on openbsd if I'm going to be maintaining them. Also makes it more likely it will run on other fringe operating systems.

@Superpat
Copy link
Contributor

For the example, I want to have a single html file in 'sdk/typescript/examples/static' that is served by each example so that they only have to serve it and then expose an sse route that updates the view.

Feel free to create that file if I dont get to it before you update your example, I'll use it for the others.

@Superpat
Copy link
Contributor

@nickchomey for your example best to follow whats discussed here: https://discord.com/channels/1296224603642925098/1296225449260879973/1333463946317009027 by @bencroker

@nickchomey nickchomey marked this pull request as draft January 29, 2025 18:37
@nickchomey
Copy link
Author

Put this into draft mode. I will work to convert this example and the existing deno.ts and node.ts examples to use the hello-world.html example that is now the standard for all sdks

@nickchomey nickchomey changed the title add isomorphic Deno-service worker example convert TS examples (deno, node, sw) to use the new hello-world.html format Jan 29, 2025
@nickchomey
Copy link
Author

I broke something while trying to merge upstream. Im just going to close this and start a new PR branch...

@nickchomey nickchomey closed this Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.