This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
🔨[refactor] [Configure Vanilla.js app to be a PWA.] #383
Open
Ankitv003
wants to merge
6
commits into
TBD54566975:main
Choose a base branch
from
Ankitv003:ankit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7079233
:hammer:[refractor][Connverted Vanilla.js app to be a PWA.]
Ankitv003 dfcacc3
:hammer:[refractor][made changes.]
Ankitv003 bc7fa79
:hammer:[refractor][made changes.]
Ankitv003 09ba2e6
Merge branch 'main' into ankit
blackgirlbytes 5ad878a
Merge branch 'main' into ankit
blackgirlbytes d5a99e0
Merge branch 'main' into ankit
blackgirlbytes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,95 @@ | ||
// main.js | ||
|
||
import { Home, About, Settings, NotFound } from './components.js'; | ||
import { Home, About, Settings, NotFound } from "./components.js"; | ||
|
||
// Define routes and their corresponding components | ||
const routes = { | ||
'/': Home, | ||
'/about': About, | ||
'/settings': Settings, | ||
"/": Home, | ||
"/about": About, | ||
"/settings": Settings, | ||
}; | ||
|
||
// Function to handle navigation | ||
function navigateTo(url) { | ||
history.pushState(null, null, url); | ||
router(); | ||
history.pushState(null, null, url); | ||
router(); | ||
} | ||
|
||
// Router function to render components based on the current URL | ||
function router() { | ||
const path = window.location.pathname; | ||
const route = routes[path] || NotFound; | ||
route(); | ||
const path = window.location.pathname; | ||
const route = routes[path] || NotFound; | ||
route(); | ||
} | ||
|
||
// Event delegation for link clicks | ||
document.addEventListener('click', (e) => { | ||
if (e.target.matches('[data-link]')) { | ||
e.preventDefault(); | ||
navigateTo(e.target.href); | ||
} | ||
document.addEventListener("click", (e) => { | ||
if (e.target.matches("[data-link]")) { | ||
e.preventDefault(); | ||
navigateTo(e.target.href); | ||
} | ||
}); | ||
|
||
// Listen to popstate event (back/forward navigation) | ||
window.addEventListener('popstate', router); | ||
window.addEventListener("popstate", router); | ||
|
||
// Initial call to router to render the correct component on page load | ||
document.addEventListener('DOMContentLoaded', router); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. registered service worker |
||
document.addEventListener("DOMContentLoaded", router); | ||
|
||
if ("serviceWorker" in navigator) { | ||
window.addEventListener("load", () => { | ||
navigator.serviceWorker | ||
.register("sw.js") | ||
.then((reg) => { | ||
console.log("Service Worker: Registered"); | ||
|
||
// Check if a new SW is waiting to activate | ||
reg.onupdatefound = () => { | ||
const newWorker = reg.installing; | ||
newWorker.onstatechange = () => { | ||
if ( | ||
newWorker.state === "installed" && | ||
navigator.serviceWorker.controller | ||
) { | ||
// Notify user about new version availability | ||
if ( | ||
confirm( | ||
"A new version of the app is available. Would you like to update?" | ||
) | ||
) { | ||
newWorker.postMessage({ action: "skipWaiting" }); | ||
} | ||
} | ||
}; | ||
}; | ||
}) | ||
.catch((err) => console.log(`Service Worker Error: ${err}`)); | ||
|
||
// Listen for `controllerchange` to reload the page when the new SW takes control | ||
navigator.serviceWorker.addEventListener("controllerchange", () => { | ||
window.location.reload(); | ||
}); | ||
}); | ||
} | ||
|
||
let deferredPrompt; | ||
|
||
window.addEventListener("beforeinstallprompt", (e) => { | ||
e.preventDefault(); | ||
deferredPrompt = e; | ||
|
||
// Show custom install button or UI (ensure an element with id="install-button" exists in your HTML) | ||
const addToHomeScreen = document.querySelector("#install-button"); | ||
addToHomeScreen.style.display = "block"; | ||
|
||
addToHomeScreen.addEventListener("click", () => { | ||
// Show the install prompt | ||
deferredPrompt.prompt(); | ||
deferredPrompt.userChoice.then((choiceResult) => { | ||
if (choiceResult.outcome === "accepted") { | ||
console.log("User accepted the install prompt"); | ||
} | ||
deferredPrompt = null; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "dwa-starter-vanillajs-vite", | ||
"display": "standalone", | ||
"scope": "/", | ||
"start_url": "/index.html", | ||
"theme_color": "#ffffff", | ||
"background_color": "#ffffff", | ||
"icons": [ | ||
{ | ||
"purpose": "maskable", | ||
"sizes": "512x512", | ||
"src": "./public/maskable-icon-512x512.png", | ||
"type": "image/png" | ||
}, | ||
{ | ||
"purpose": "any", | ||
"sizes": "512x512", | ||
"src": "./public/pwa-512x512.png", | ||
"type": "image/png" | ||
} | ||
], | ||
"orientation": "any", | ||
"dir": "auto", | ||
"lang": "en-US" | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line is missing only