-
-
Notifications
You must be signed in to change notification settings - Fork 213
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
Possiblity to make it work on fresh laravel build or a working sample? #431
Comments
I have same issue trying to convert a Vite + Vue + Laravel project to PWA -Following |
same problem what to do? |
@msonowal you can use It seems you are using TypeScript in your custom service worker, add |
Here the reference: https://developer.chrome.com/docs/workbox/reference/workbox-build/#type-RequiredGlobDirectoryPartial You'll need to also add |
@userquin How about an fork with laravel + inertiajs integration with Vite-PWA support? |
Has anyone got this working? For me, I'm not getting anything injected into the html - no service workers, no manifest...
|
I'm sorry folks, I dont use laravel. |
Laravel projects are based on this and use the same folder structure https://github.com/laravel/laravel Static files including js, vue files ect. are all put in the resources directory and then generated into the public/build/ folder by vite. |
You can try to add this to pwa options: plugins: [
VitePWA({
outDir: 'public/build',
})
], |
You're kidding! Thank you, that's saved a lot of time. For anyone else wondering, even though some files get generated in public/build you also need registerSW.js and workbox to be generated there - adding It's still not auto injecting the script and manifest for me, but I've gotten around that by manually including them in the blade layout:
|
@fredpedersen provide these pwa options: plugins: [
VitePWA({
outDir: 'public/build',
scope: '/',
base: '/',
/* other options*/
manifest: {
id: '/',
scope: '/',
/* other options*/
}
})
], |
@fredpedersen so the root/base for the app is |
@userquin the root of the app is If the directory is like |
@anburocky3 @fredpedersen can you try using this? plugins: [
VitePWA({
outDir: 'public',
})
], EDIT: this way you don't need to switch to manual injection, and you will be able to use VanillaJS virtual module for auto update and auto page reload. |
Is there any way that, we can inject the script imports directly to our blade layout? |
I think, it is generating correctly, but it doesn't get inserted into It will be cool, if it auto-imports the necessary scripts in our app. FYI, this is the layout file: <!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title inertia>{{ config('app.name', 'Laravel') }}</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<!-- Scripts -->
@routes
@vite('resources/js/app.ts')
@inertiaHead
</head>
<body class="antialiased">
@inertia
</body>
</html> |
Check my previous comment, I add EDIT entry: then in your layour you can just use in import { registerSW } from 'virtual:pwa-register'
/*other code*/
registerSW({ immediate: true }) |
For the web manifest you can use this another virtual import { pwaInfo } from 'virtual:pwa-info'
if (pwaInfo) {
const webManifest = pwaInfo.webManifest.linkTag
console.log(pwaInfo)
/* add link to head: webManifest is the link */
} |
for example, we use it in SvelteKit layout: https://github.com/vite-pwa/sveltekit/blob/main/examples/sveltekit-ts/src/routes/%2Blayout.svelte |
Based on suggestion, tried it. My Config: VitePWA({
registerType: 'autoUpdate',
devOptions: {
enabled: true
},
// outDir: 'public/build',
outDir: 'public',
scope: '/',
base: '/',
includeAssets: ['favicon.ico', 'apple-touch-icon.png', 'masked-icon.svg'],
manifest: {
id: '/',
scope: '/',
name: 'MenuWorkflow',
short_name: 'MenuWorkflow',
description: 'Make order workflow ease.',
theme_color: '#ffffff',
icons: [
{
"src": "/img/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "/img/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "/img/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "/img/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "/img/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "/img/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "/img/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png",
"purpose": "maskable any"
},
{
"src": "/img/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable any"
}
]
}
}) Issues found: I mainly use PWA, to add this app in all native platforms. Is there any way, we can show |
@anburocky3 where is |
From some testing:
also breaks it, saying trying to install regardless of manual or automatic injection I now have a running and active service worker with manual injection and with:
But it's throwing the error EDIT: |
I'm using Laravel app, as discussed above, the entry point of the app is in
Can you share the entire config files, so it will be easy to understand. |
@anburocky3 add it also on |
@anburocky3 check the build folder and remove |
|
about https://github.com/vite-pwa/astro/blob/main/examples/pwa-prompt/src/layouts/DefaultLayout.astro#L31 |
Here's the full config at the moment:
The current issue with that config is that it seems to be generating sw.js and workbox at |
so Vite build is changed to public/build folder, revert outDir to public/build and remove sw.js, registerSW.js and workbox-*.js from public folder |
@fredpedersen type error on this. How did you get rid of it? With respect, @userquin if we register the |
@ibarral maybe this headers preventing cache |
@userquin I just changed it to "cache-control: public" with no luck either :( Here is the new URL... the old one has expired: Thanks a lot!!! |
@ibarral I'll review my docs to modify the runtimeCaching plugin and so we can debug it |
@userquin ok thank you very much! I will wait for your feedback ;) |
@MatheusRmelo can you make a working example? I tried to implement this in my project, I think I spend around 5 hours and had no results. I gave up later on and defaulted to https://github.com/silviolleite/laravel-pwa but I wished I could make this work since I think the developer makes good work. |
@sibalonat Hi, Sorry for late. I did a example in https://github.com/MatheusRmelo/laravel-pwa-with-vue. |
@MatheusRmelo what could be the reason why |
So after many hours of trial and error, I came up with this solution: VitePWA({
registerType: 'autoUpdate',
injectRegister: 'script',
manifestFilename: '../manifest.webmanifest', // this ugly workaround to get in public root
outDir: 'public',
base: 'public',
scope: '/',
buildBase: '/',
workbox: {
navigateFallback: '/',
navigateFallbackDenylist: [/\/[api,admin]+\/.*/],
maximumFileSizeToCacheInBytes: 4194304,
globPatterns: ['**/*.{css,js,html,svg,png,ico,txt,woff2}'],
cleanupOutdatedCaches: true,
directoryIndex: null, // this prevents fallback to index.html
},
manifest: {
name: 'App',
short_name: 'App',
description: 'App',
theme_color: '#39336c',
background_color: '#39336c',
orientation: 'portrait-primary',
id: '/',
scope: '/',
start_url: '/',
icons: [
{
src: '/storage/images/android-chrome-192x192.png',
sizes: '192x192',
type: 'image/png'
},
{
src: '/storage/images/android-chrome-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'any maskable'
}
]
},
}) On your <link rel="manifest" href="/manifest.webmanifest">
<script id="vite-plugin-pwa:register-sw" src="/build/registerSW.js"></script> @userquin I could enable dev mode, but this doesn't work when you want to deploy, right? Also I'm I missing something? It seems the build directory is being forced somehow. Thanks |
EDIT: about dev, yes, you can ignore dev-dist folder. |
@userquin Thanks, I tried using Hmm, if one uses dev, it does include developer tools to work, right? I don't think this is wanted for production. |
You need to add the http header in your backend to instruct the browser the sw scope is / being served from build folder. EDIT: a service worker can only intercept resources/request from its base (in your case |
@userquin Ah :) Could you please give a full example:
Should you use try_files, or do you know an efficient solution? Thanks |
No, sorry, I don't use Laravel, I use only Netty or Apache HTTP Server via proxy in dev (Vite/Nuxt + Kotlin Backend): check https://vite-pwa-org.netlify.app/deployment/ , there are some hints and a few servers there |
@userquin Many Thanks for the help, it seems to be working. :) Full solution:
The
And this in
|
You need an html page in the dist folder if using offline support (SW precaching): check point 6.6 here https://vite-pwa-org.netlify.app/guide/cookbook.html#vite-plugin-pwa-closebundle-hook Since html pages being served by the backend workbox will not find it: try adding workbox.additionalManifestEntries = [{ url : '/', revision: `${Date.now()}` }] EDIT: your backend should return the entry point when requesting |
@userquin My first goal was to make it installable. Which seems to be possible now. Hmm, doesn't the DirectoryIndex null solve this issue? Didn't know you could manually add records. :) I kinda new to offline support. It would be great if assets could be cached, however it seems even be possible to 'save' a video stream to the client, which is really cool. Thanks for the help, really appreciate it. |
upps, sorry, you've navigationFallback disabled, IIRC the PWA will not be installable (too many options out there), just try it (check if the install button appears in the url address) |
@userquin Yep, it works. The amount of options are really overwhelming when you need to generate the code yourself. I'm really glad your package exists. :) |
Thanks everyone for the epic amount of information and debugging in this thread. For those using Sail, I was able to add the Service-Worker-Allowed header by overriding laravel/framework/Illuminate/Foundation/Resources/server.php by adding a file called <?php
$publicPath = getcwd();
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ?? ''
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && $uri != '/build/sw.js' && file_exists($publicPath.$uri)) {
return false;
}
if ($uri == '/build/sw.js') {
header('Service-Worker-Allowed: /');
header('Content-Type: text/javascript');
echo file_get_contents(__DIR__ . '/public/build/sw.js');
exit;
}
require_once $publicPath.'/index.php'; |
I found that the static icons in the Laravel public folder and those specified in To solve this, I don't want to suggest more work with workbox: {
additionalManifestEntries: [
{ url: '/favicon.ico', revision: `${Date.now()}` }
]
} and that worked fine. |
Hi guys, are there any definitive working config for a laravel + vue3 (ts) + vite setup resulting from this thread? Any of you got the complete pwa experience working with offline support and app installation? Thanks to everybody that has added their setups here but I am currently lost on what is the correct config to use. I would appreciate any help from the people that got it to work! |
@rzuzo It should work with the Vue + Vite examples given already. Did you actually try something? |
It works for me too. I was about to add my vite.config.ts here, but I realised there's a bit more going on. So I've set up a demo repo to share what I did - I've put it here and will work on it now: https://github.com/sfreytag/laravel-vite-pwa |
I think I am done - have added a few docs to the README to explain, but as a quickstart here's the diff between the vanilla Laravel 10 install and the current HEAD, showing all the things added to achieve Vite + vite-plugin-pwa + Vue + Typescript with offline support and install prompts, etc - https://github.com/sfreytag/laravel-vite-pwa/compare/a59497..HEAD |
By default the `laravel-vite-plugin` will overwrite the sw build directory to `public/build` dir, and someone find it incorrect, see vite-pwa/vite-plugin-pwa#547 or even couldn't get it working at all vite-pwa/vite-plugin-pwa#431. wandering around the community I come across the `vite.config.js` [^1] to move the build scope to root folder, as consequence we need to configure our server to allow service worker to run on root public directory while the `sw.js` located in `build` directory. As of now, I still need to ensure how it works while I learn and make the service worker working in the first place [^1]: https://github.com/sfreytag/laravel-vite-pwa/blob/ecbdb05c1935040737b2c57ee0e2690f784e7a2c/vite.config.js\#L62-L154 Signed-off-by: Fery Wardiyanto <[email protected]>
You guys are all legends. Thank you so much! |
Feel free to send a PR to the docs repo: maybe adding a new page in the frameworks folder called |
By default the `laravel-vite-plugin` will overwrite the sw build directory to `public/build` dir, and someone find it incorrect, see vite-pwa/vite-plugin-pwa#547 or even couldn't get it working at all vite-pwa/vite-plugin-pwa#431. wandering around the community I come across the `vite.config.js` [^1] to move the build scope to root folder, as consequence we need to configure our server to allow service worker to run on root public directory while the `sw.js` located in `build` directory. As of now, I still need to ensure how it works while I learn and make the service worker working in the first place [^1]: https://github.com/sfreytag/laravel-vite-pwa/blob/ecbdb05c1935040737b2c57ee0e2690f784e7a2c/vite.config.js\#L62-L154 Signed-off-by: Fery Wardiyanto <[email protected]>
I use the fresh default laravel vite config which can be re-produced by creating a new project with laravel
and install jetstream with inertia stack.
Now try to configure the service worker and plugin,
As per my tries and findings it generates the paths incorrectly which refers to the build folder.
This is my current vite config
The text was updated successfully, but these errors were encountered: