React App hosted in Firebase Hosting - Uncaught SyntaxError: Unexpected token '<' - reactjs

I've seen at least 4 threads with the same problem and tried solutions from them, but none of them worked for me.
When I deploy my react app to firebase hosting I get blank page and a following error:
The only way to get it working is to empty cache and hard reload, but then reloading it again would cause exactly the same error.
I double checked my build html gets assets from correct folders using %PUBLIC_URL% as part of the routes in public/index.html
I also commented out manifest and the whole registering of the serviceWorker function, then rebuild it and deployed it, but I still have exactly the same problem.
At this point I'm a bit dumbfounded cause I think it was caused by whole caching system and now that I disabled it, it still remains so I don't know anymore how this whole thing actually works
My firebase.json
{
"hosting": {
"public": "build",
"site": "react-pukinn",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{ "source":"/serviceWorker.js", "headers": [{"key": "Cache-Control", "value": "no-cache"}] }
]
}
}
My serviceWorker.js:
let CACHE_NAME = 'pukinn';
const urlsToCache = [
'/',
'/index.html',
];
self.addEventListener('install', (event) => {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(caches.match(event.request)
.then(function(response) {
if (response) {
return response;
}
return fetch(event.request);
})
);
});
self.addEventListener('install', (event) => {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
self.skipWaiting();
});
manifest.json
{
"short_name": "Púkinn",
"name": "Púkinn",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "icons/knight_demon-192x192-min.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "icons/knight_demon-512x512-min.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": "index.html",
"display": "standalone",
"theme_color": "#ed1c24",
"background_color": "#212121"
}

I found the problem. My serviceWorker.js file was in the root of the project instead of my ./src folder. So what happed is that on build it did not include it into build folder that is used for hosting.
Really dumb mistake on my part.

Related

what is the use of manifest.json in react.js

My firebase hosted page is rendering a blank page. The console says there are some problem in manifest.json. Can anyone please help in figuring it out.
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
The errors in console are
I was able to debug and solve the issue, actually there was some misconfiguration in the firebase installation.
Deleted firebase.json
run firebase init
Set the deploy target as from https://firebase.google.com/docs/cli/targets
firebase target:apply TYPE TARGET_NAME RESOURCE_IDENTIFIER
Where the parameters are:
TYPE — the relevant Firebase resource type
For Firebase Hosting sites, use hosting.
TARGET_NAME — a unique name for the Hosting site that you're deploying to
RESOURCE_IDENTIFIER — the SITE_ID for the Hosting site as listed in your Firebase project
For example, if you've created two sites (myapp-blog and myapp-app) in your Firebase project, you could apply a unique TARGET_NAME (blog and app, respectively) to each site by running the following commands:
firebase target:apply hosting blog myapp-blog
firebase target:apply hosting app myapp-app
Then created deploy target in the array of firebase.json
{
"hosting": [ {
"target": "blog", // "blog" is the applied TARGET_NAME for the Hosting site "myapp-blog"
"public": "blog/dist", // contents of this folder are deployed to the site "myapp-blog"
// ...
},
{
"target": "app", // "app" is the applied TARGET_NAME for the Hosting site "myapp-app"
"public": "app/dist", // contents of this folder are deployed to the site "myapp-app"
// ...
"rewrites": [...] // You can define specific Hosting configurations for each site
}
]
}
{
"storage": [ {
"target": "main", // "main" is the applied TARGET_NAME for the group of Cloud Storage buckets
"rules": "storage.main.rules" // the file that contains the shared security rules
}
]
}
The I used,
npm run build
6. Then run,
firebase deploy

Cannot register service worker on react serverless

I am trying to register a service worker to make a react application PWA, when deployed on GitHub pages.
The error I am getting is:
No matching service worker detected. You may need to reload the page, or check that the scope of the service worker for the current page encloses the scope and start URL from the manifest.
This is the manifest.json:
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": "https://[github-username].github.io/",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
In packages.json:
"homepage": "https://[githubid].github.io/[repository]",
Both service-worker.js and register-serviceWorker.js are located in src folder.
I have tried to build it locally and serve it with npm serve, and the browser identifies the service worker.
My belief is that the scope and start url are not ok configured in manifest.json.
I have added the following to manifest.json:
"start_url": "https://[gitHubId].github.io/",
"scope": "/",
It now works.
I was getting the same issue in dev. env. even tho i run the build with serve package .
The solution was to export NODE_ENV=production as environment variable.
or we can simply remove production condition in service worker registration file.
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator)

next-pwa sw.js redundant and not working on Vercel

I am working on NextJS app that runs next-pwa but only works on local environment. But when I deploy to vercel, it seems that sw.js is redundant. I spent so many hours on this but feel stuck anyway.
Here's my next.config.js
const { withPlugins } = require("next-compose-plugins");
const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");
const nextConfig = {
env: {
API_URL: "https://multikart-graphql-dun.vercel.app/server.js",
},
// if you want to run with local graphQl un-comment below one and comment the above code
// env: {
// API_URL: "http://localhost:4000/graphql",
// },
webpack(config, options) {
config.module.rules.push({
test: /\.svg$/,
issuer: {
test: /\.(js|ts)x?$/,
},
use: ["#svgr/webpack"],
});
return config;
},
};
module.exports = withPlugins(
[
[withImages],
[
withPWA,
{
// reactStrictMode: true,
pwa: {
dest: "public",
sw: "sw.js",
// swSrc: "service-worker.js",
// scope: "/",
// register: true,
// skipWaiting: true,
// buildExcludes: [/middleware-manifest.json$/],
},
},
],
],
[nextConfig]
);
and here's my manifest.json
{
"short_name": "App",
"name": "App: App",
"start_url": "/",
"display": "standalone",
"theme_color": "#b8d8e6",
"background_color": "#b8d8e6",
"prefer_related_applications": true,
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "/icons/imagedummy192.png",
"type": "image/png",
"sizes": "192x192",
"purpose": "any maskable"
},
{
"src": "/icons/imagedummy512.png",
"type": "image/png",
"sizes": "512x512"
}
]
}
I commented the rest of the config (runTimeCaching, skipWaiting, register), since it makes no difference
I have followed https://github.com/vercel/next.js/issues/30692 and https://github.com/shadowwalker/next-pwa/issues/295
I also have followed basic config on this repo: https://github.com/shadowwalker/next-pwa/tree/master/examples/minimal
I also did turn off the adblocker
but nothing seems to work, and sw.js always redundant
Can anybody help, What did I do wrong? 🙏
I found an answer to the question.
So to make the service worker works, I needed to build the service worker on production build ( npx next build ).
After the build complete, I remove the service worker on .gitignore to make it deployed to the repository.
And then voilaa.. the next-pwa works. Although, it needs 10 minutes for service worker success to build.

Deploy React App and Express API on Vercel

I want to deploy a project that includes a React App as frontend (client/) and an Express API backend (api/) on vercel. I'm able to deploy the api but how can I deploy also the client?
My project structure:
-api/
-package.json
-index.js
-client/
-package.json
-public/...
-src/...
-vercel.json
My current vercel.json:
{
"version": 2,
"builds": [
{
"src": "./api/index.js",
"use": "#vercel/node"
},
// that is what I tried but it's not working:
{
"src": "./client/package.json",
"use": "#vercel/static-build"
}
// End of the non-functional code
],
"routes": [
{
"src": "api/.*",
"dest": "api/index.js"
},
// that is what I tried but it's not working:
{
"src": "client/build/static/.*",
"dest": "static/.*"
},
{
"src": "client/build/index.html",
"dest": "index.html"
}
// End of the non-functional code
]
}

web manifest: Line: 1, column: 1, Syntax error

I have react + webpack app
My manifest.webmanifest
{
"name": "x",
"short_name": "x",
"start_url": "./index.html",
"display": "fullscreen",
"background_color": "white",
"theme_color": "white",
"icons": [
{
"src": "favicon-16x16.png",
"type": "image/png",
"sizes": "16x16"
},
{
"src": "favicon-32x32.png",
"type": "image/png",
"sizes": "32x32"
}
]
}
Index.html
<link crossorigin="use-credentials" rel="manifest" href="manifest.webmanifest" />
I tried different combination but always get error in Chorome (at localhost):
Line: 1, column: 1, Syntax error;
In which folder should manifest.webmanifest be? (src, public, public/img ????) index.html is in src folder
What to write in href in index.html? (href="manifest.webmanifest", href="./manifest.webmanifest" ???)
What to write in 'start_url' in manifest.webmanifest?
Where to store favicon-16x16.png and favicon-32x32.png (src, public ???)
What to write in icons src in manifest.webmanifest
"icons": [
{
"src": ?????????,
"type": "image/png",
"sizes": "16x16"
},
Do I need to write something additional in the settings of Webpack?
I found the answer!))
The problem was that Webpack does not include manifest.webmanifest and icons img in result bundle. We need do it manually, writing the necessary paths in Webpack config:
add const CopyWebpackPlugin = require('copy-webpack-plugin'); at webpack.config.js
...
plugins: [
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, 'src/manifest.webmanifest'),
to: path.resolve(__dirname, 'build'),
},
{
from: path.resolve(__dirname, 'public/img/icons/'),
to: path.resolve(__dirname, 'build/icons/'),
},
],
}),
...
now all images and manifest.webmanifest are in result build folder!

Resources