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

[Content]: Request to add short tutorial for adding service workers. #929

Open
MengLinMaker opened this issue Oct 21, 2024 · 1 comment
Open
Assignees
Labels
help wanted Looking for assistance on this issue

Comments

@MengLinMaker
Copy link

📚 Subject area/topic

Service Worker

📋 Page(s) affected (or suggested, for new content)

/solid-start/reference/entrypoints/entry-client.mdx

📋 Description of content that is out-of-date or incorrect

You can register service workers in entry-client.tsx

Here's how you create a service worker with raw JavaScript:
Write service worker file and place this in the public folder. eg public/sw.js
In entry-client.tsx, register your service worker:

import { StartClient, mount } from '@solidjs/start/client'

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js', { scope: '/' }).then(
    (registration) => {
      console.log('Service worker registration succeeded:', registration)
    },
    (error) => {
      console.error(`Service worker registration failed: ${error}`)
    },
  )
} else {
  console.error('Service workers are not supported.')
}

mount(() => <StartClient />, document.getElementById('app')!)

Now the service worker should apply to all paths after /

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

@MengLinMaker MengLinMaker added the pending review Awaiting review by team members. label Oct 21, 2024
@MengLinMaker
Copy link
Author

I've used this in my own solid start app.
Since I've used service workers before, this wasn't too hard to figure out.
But I'd imagine this is not intuitive for those who are unfamiliar with service workers.

@MengLinMaker MengLinMaker reopened this Oct 21, 2024
@LadyBluenotes LadyBluenotes added help wanted Looking for assistance on this issue and removed pending review Awaiting review by team members. labels Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Looking for assistance on this issue
Projects
None yet
Development

No branches or pull requests

2 participants