Firebase Hosting Automatic Cache Busting with Bundle hash change in .js file /service-worker.js - reactjs

At the moment I am using this setup in my firebase.json file, the service-worker.js file would cause the bundles to be cleared out of cache and downloaded fresh but it required a hard control+R refresh to do so, it didn't happen automatically, and my wildcard pattern matching on **/*.#(js|css) is now causing my rule for no-cache on /service-worker.js to not work either, but it is no good to force the user to hard refresh control+R whenever I deploy the next version, I need a better solution than setting max-age on my bundle to 0.
Any ideas?
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**/*.#(eot|otf|ttf|ttc|woff|font.css)",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
},
{
"source": "**/*.#(js|css)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=86400"
}
]
},
{
"source": "**/*.#(jpg|jpeg|gif|png)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=604800"
}
]
},
{
"source": "/service-worker.js",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache"
}
]
}
]
}
}

I had similar question but I figured out how to. NOTE in my case Service Worker file is sw.js. You can find my config below. I just added separate rule for sw.js before wild card rule matching all the JS and CSS files:
{
"hosting": {
"public": "public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**/*.#(jpg|jpeg|gif|png|svg|ico)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=7200"
}
]
},
{
"source": "sw.js",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=0"
}
]
},
{
"source": "*/*.#(js|css)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=3600"
}
]
},
{
"source": "manifest.json",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=86400"
}
]
}
]
}
}

Related

"Cannot read property 'includes' of undefined" with Gatsby

I'm trying to host my Gatsby site on Firebase, but when I run the "gatbsy build" I get this error in my terminal:
ERROR #98123 WEBPACK
Generating JavaScript bundles failed
Cannot read property 'includes' of undefined
not finished Generating image thumbnails
Here's my firebase.json file:
{
"hosting": {
"public": "public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"headers": [
{
"source": "**/*",
"headers": [
{
"key": "cache-control",
"value": "public, max-age=0, must-revalidate"
}
]
},
{
"source": "static/**",
"headers": [
{
"key": "cache-control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "**/*.#(css|js)",
"headers": [
{
"key": "cache-control",
"value": "public, max-age=31536000, immutable"
}
]
},
{
"source": "sw.js",
"headers": [
{
"key": "cache-control",
"value": "public, max-age=0, must-revalidate"
}
]
},
{
"source": "page-data/**",
"headers": [
{
"key": "cache-control",
"value": "public, max-age=0, must-revalidate"
}
]
}
]
}
}
While my package-lock.json generates:
"array-includes": {
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.1.3.tgz",
"integrity": "sha512-gcem1KlBU7c9rB+Rq8/3PPKsK2kjqeEBa3bD5kkQo4nYlOHQCJqIJFqBXDEfwaRuYTT4E+FxA9xez7Gf/e3Q7A==",
"requires": {
"call-bind": "^1.0.2",
"define-properties": "^1.1.3",
"es-abstract": "^1.18.0-next.2",
"get-intrinsic": "^1.1.1",
"is-string": "^1.0.5"
}
},
And these are the other files where "includes" is declared:
return (
<Wrapper ref={headerRef}>
<Heading
lineColor={lineColor}
isActive={title.toLowerCase().includes(activeLink)}
>
{title}
</Heading>
<Paragraph>{paragraph}</Paragraph>
</Wrapper>
);
const doesConnectionSupportPrefetch = () => {
if (`connection` in navigator && typeof navigator.connection !== `undefined`) {
if ((navigator.connection.effectiveType || ``).includes(`2g`)) {
return false;
}
if (navigator.connection.saveData) {
return false;
}
}
return true;
};
I've tried some possible solutions like updating Node, Babel and clearing up my cache. But no success yet. Any suggestion, please?

An error occurred while requesting the visualisation's config resource

I got an error "An error occurred while requesting the visualisation's config resource" while trying to request my custom visualisation. All files have been made public.
manifest.json
{
"name": "Custom Vis",
"description": "By Sorrow",
"devMode": true,
"components": [{
"id": "Custom Vis",
"name": "Custom Vis",
"iconUrl": "https://raw.githubusercontent.com/googledatastudio/community-visualizations/master/docs/codelab/img/bar_chart.png",
"description": "Custom Vis",
"resource": {
"js": "custom-vis/final.js",
"config": "custom-vis/config.json",
"css": "custom-vis/style.css"
}
}]
}
config.json
{
"data": [
{
"id": "concepts",
"label": "Concepts",
"elements": [
{
"id": "barDimension",
"label": "Dimension",
"type": "DIMENSION",
"options": {
"min": 1,
"max": 1
}
},
{
"id": "barMetric",
"label": "Metric",
"type": "METRIC",
"options": {
"min": 1,
"max": 1
}
}
]
}
],
"style": [
{
"id": "color",
"label": "Colors",
"elements": [
{
"type": "FONT_COLOR",
"id": "barColor",
"label": "Bar Color",
"defaultValue": "black"
}
]
}
]
}
Can someone tell me why it doesn't work?
You need to create a folder on the Google Cloud where your files will go:
Then add it like here:
"resource": {
"js": "gs://yourname/index.js",
"config": "gs://yourname/index.json",
"css": "gs://yourname/index.css"
}
PS: watch a video here https://developers.google.com/datastudio/visualization/local-dev
That will save you a lot of time on development.
I stumbled upon the same issue.
Resources were provided in the default tutorial in a form of
"resource": {
"js": "MY_GOOGLE_CLOUD_STORAGE_BUCKET/myViz.js",
"config": "MY_GOOGLE_CLOUD_STORAGE_BUCKET/myViz.json",
"css": "MY_GOOGLE_CLOUD_STORAGE_BUCKET/myViz.css"
}
What was not clear is that they actually need to include gs:// in the path. Would be clearer if they explicitly mentioned it like so:
"resource": {
"js": "gs://MY_GOOGLE_CLOUD_STORAGE_BUCKET/myViz.js",
"config": "gs://MY_GOOGLE_CLOUD_STORAGE_BUCKET/myViz.json",
"css": "gs://MY_GOOGLE_CLOUD_STORAGE_BUCKET/myViz.css"
}
It must work this way.

Firebase - Request header field x-firebase-gmpid is not allowed by Access-Control-Allow-Headers in preflight response

My product uses firebase rtdb, firestore, storage, auth, and hosting.
I didn't make any changes to my CORS configuration. However, today I started getting the following CORS error while trying to upload images to storage, and retrieve them:
Access to XMLHttpRequest at 'https://firebasestorage.googleapis.com/v0/b/diary-a77f6.appspot.com/o?name=images%2FJ1gU3KPfo1cTHJXhT3iopBqrvVs1%2F-M1Ah4xCQ46GvEZtwgR1%2FBalboa%20Pier%2C%20California%20-%201600x1200%20-%20ID%2027253.jpg' from origin 'https://daybook.app' has been blocked by CORS policy: Request header field x-firebase-gmpid is not allowed by Access-Control-Allow-Headers in preflight response.
I tried Firebase Storage and Access-Control-Allow-Origin,
https://cloud.google.com/storage/docs/configuring-cors, and https://firebase.google.com/docs/storage/web/download-files#cors_configuration to no avail.
My hosting config:
{
"target": "prod",
"public": "build/production",
"ignore": [
"firebase.json",
"src/firebase/keys.js",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "/**",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache, no-store, must-revalidate"
},
{
"key": "Access-Control-Allow-Headers",
"value": "x-firebase-gmpid, Origin, Accept, Content-Type, X-Requested-With, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization"
}
]
},
{
"source": "**/*.#(css|js)",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache, no-store, must-revalidate max-age=0"
}
]
},
{
"source": "**/*.#(eot|otf|ttf|ttc|woff|font.css)",
"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
},
{
"source": "**/*.#(jpg|jpeg|gif|png|webp|webp|svg)",
"headers": [
{
"key": "Cache-Control",
"value": "max-age=7200"
}
]
}
]
}
This is what I added recently while trying to fix the issue:
{
"key": "Access-Control-Allow-Headers",
"value": "x-firebase-gmpid, Origin, Accept, Content-Type, X-Requested-With, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization"
}
I think this was an issue with the Firestore JS client library. I rolled it back to version 7.9.0 from version 7.9.2 and the error went away.
edit: 7.9.3 contains the fix.

Combining React router and Firebase function api

i would like to know if there is a way to use both react-router-dom and firebase functions. For example the React part has a few links like mywebsite.com/pageone, mywebsite.com/pagetwo, mywebsite.com/pagethree. I want to add a mywebsite.com/api link that comes from a firebase function. I looked up the firebase documentation but it only shows to do either one or the other. Is this possible? Here is the firebase.json
{
"hosting": {
"public": "build",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
},
{
"source": "/app/api",
"function": "app"
}
]
}
}
Try changing the order of the rewrites so the wildcard is at the end. That solved it for me.
From:
"rewrites": [
{
"source": "**",
"destination": "/index.html"
},
{
"source": "/app/api",
"function": "app"
}
]
To:
"rewrites": [
{
"source": "/app/api",
"function": "app"
},
{
"source": "**",
"destination": "/index.html"
}
]

Some Modes on action.devices.traits.Modes does not seem to be working

I'm playing around with action.devices.traits.Modes trait, and it seems that I cannot get some Mode to work, though I'm 100% sure that I've used mode names exactly as in the documentation.
SYNC Response:
Response {
"status": 200,
"headers": {
"content-type": "application/json;charset=utf-8"
},
"body": {
"requestId": "15896565509066911859",
"payload": {
"agentUserId": "",
"devices": [
{
"id": "Washer",
"type": "action.devices.types.WASHER",
"traits": [
"action.devices.traits.Modes",
"action.devices.traits.OnOff"
],
"name": {
"defaultNames": [
"Washer"
],
"name": "Washer",
"nicknames": [
"Washer"
]
},
"willReportState": false,
"customData": {
"lookupOnExecute": true
},
"attributes": {
"availableModes": [
{
"name": "temperature",
"name_values": [
{
"name_synonym": [
"temperature"
],
"lang": "en"
}
],
"settings": [
{
"setting_name": "Tap Cold",
"setting_values": [
{
"setting_synonym": [
"tap cold"
],
"lang": "en"
}
]
},
{
"setting_name": "Cold Warm",
"setting_values": [
{
"setting_synonym": [
"cold warm"
],
"lang": "en"
}
]
},
{
"setting_name": "Hot",
"setting_values": [
{
"setting_synonym": [
"hot"
],
"lang": "en"
}
]
},
{
"setting_name": "Extra Hot",
"setting_values": [
{
"setting_synonym": [
"extra hot"
],
"lang": "en"
}
]
}
],
"ordered": false
},
{
"name": "Cycle",
"name_values": [
{
"name_synonym": [
"cycle"
],
"lang": "en"
}
],
"settings": [
{
"setting_name": "Low",
"setting_values": [
{
"setting_synonym": [
"low"
],
"lang": "en"
}
]
},
{
"setting_name": "Medium",
"setting_values": [
{
"setting_synonym": [
"medium"
],
"lang": "en"
}
]
},
{
"setting_name": "High",
"setting_values": [
{
"setting_synonym": [
"high"
],
"lang": "en"
}
]
}
],
"ordered": false
}
]
}
}
]
}
}
}
QUERY response:
Response {
"status": 200,
"headers": {
"content-type": "application/json;charset=utf-8"
},
"body": {
"requestId": "17335788264128789760",
"payload": {
"devices": {
"Washer": {
"online": true,
"currentModeSettings": {
"Cycle": "High",
"temperature": "Tap Cold"
},
"on": true
}
}
}
}
}
Why does Cycle query fail? I see Cycle mode in the documentation. Does it support only some settings? Does the case matter? High is also available, so what am I doing wrong?
And Interaction with Google Home:
While the Google Assistant is highly capable of detecting variations in speech while receiving voice commands, it is still a good practice to make the grammar more understandable and as mentioned in the comments. This is related to an issue in the usage of grammar while triggering the right intent, since a change in the way the question was asked solved the purpose.
For the answer to the question "how can I combine Two toggles (Bluetooth and Power Saving) into one trigger – e.g. "Turn on Power Saving and Bluetooth on Home Cinema?" asked in the comments – at the moment Smart Home doesn't allow two commands in a single prompt. For example, you cannot say "Hey Google, turn on my light and change the color". In order to do that you have to execute each command separately.

Resources