How to add Leverage browser caching to firebase.json - json

I'm using Firebase on Google Cloud Platform for the first time and I've uploaded my static website but now I'd like to add:
"headers": [ {
"source" : "**/*.#(eot|otf|ttf|ttc|woff|font.css)",
"headers" : [ {
"key" : "Access-Control-Allow-Origin",
"value" : "*"
} ]
}, {
"source" : "**/*.#(jpg|jpeg|gif|png)",
"headers" : [ {
"key" : "Cache-Control",
"value" : "max-age=7200"
} ]
}, {
// Sets the cache header for 404 pages to cache for 5 minutes
"source" : "404.html",
"headers" : [ {
"key" : "Cache-Control",
"value" : "max-age=300"
} ]
} ]
to enable Leverage browser caching, but I do not understand how to add these lines of code to the firebase.json file?
The firebase init command creates a firebase.json settings file in the root of your project's directory, but how can I change it after I've created the site?
Thanks a lot

If you want to change the caching settings on your web site, change the relevant Change-Control header in your firebase.json and then rerun firebase deploy. This will deploy the latest firebase.json with your new settings, and ensure that all HTML/CSS/JS/etc files are up to date too.
If you lost your firebase.json, a simple default could look something like this from the Firebase Hosting reference documentation:
{
"hosting": {
"public": "app",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}

Related

Redirects in declarativeNetRequests to Local Resource

I am trying to redirect the request sent to https://site/script1.js to a modified script stored locally chrome-extension://ext_id/my_script.js.
(Basically trying to emulate the behaviour of webRequests.onBeforeRequest -> return redirect to local file)
My rules.json :
[
{
"id": 1,
"priority": 10,
"action":
{ "type": "redirect",
"redirect" : {"extensionPath" : "/my_script.js"}
},
"condition":
{"urlFilter": "https://site/script1.js",
"resourceTypes": ["script"]
}
}
]
If I change the action type to block the chrome dev tools shows the request as blocked, and the website doesnt load, however on redirects, nothing happens and the request goes through. It doesnt work for Images either.
Is there possibly a workaround for this (to modify the script a site receives from a request)?
Edit:
I'll just try to get an image to work first, the declarativeNetRequest Docs single out Redirects for a script.
The website (https://www.example.com/a/b) sends a request to https://cdn.example.com/file.jpg. I want that request to return a file `my_file.jpg' stored locally .
I have :
Rules.json
[
{
"id": 2,
"priority": 9,
"action":
{ "type": "redirect",
"redirect" : {"extensionPath" : "/my_file.jpg"}
},
"condition":
{"urlFilter": "https://cdn.example.com/file.jpg",
"resourceTypes": ["image"]
}
}
]
and Manifest.json includes:
"host_permissions": [
"https://www.example.com/*"
],
"web_accessible_resources":[
{
"resources": ["my_file.jpg"],
"matches" :["https://www.example.com/*"]
}
]

Migrating webRequest to declarativeNetRequest

Hi I was using webRequest in manifest V2 , I started getting below error
'webRequestBlocking' requires manifest version of 2 or lower.
So I am trying to convert my below existing code to declarativenetrequest
var responseListener = function (details) {
var rule = {
name: "Access-Control-Allow-Origin",
value: "*",
};
details.responseHeaders.push(rule);
var rule1 = {
name: "Access-Control-Allow-Methods",
value: "GET, PUT, POST, DELETE, HEAD, OPTIONS",
};
details.responseHeaders.push(rule1);
return { responseHeaders: details.responseHeaders };
};
chrome.webRequest.onHeadersReceived.addListener(
responseListener,
{
urls: [
"https://example.com/*",
"*://*.example1.com?test",
],
},
// extraInfoSpec
["blocking", "responseHeaders", "extraHeaders"]
);
So i tried replacing to chrome.declarativeNetRequest.onHeadersReceived.addListener(
And I got Uncaught TypeError: Cannot read properties of undefined (reading 'addListener').Can some one help me how the current function can be migrated to declarativeNetRequest.
The error you're seeing is because declarativeNetRequest provides a different API to webRequest and there are some additional steps for migration. You can learn more about the API in general here: https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/
For your use case, you can actually setup a rule declaratively in the manifest without needing to call any APIs at all! This is one of the benefits of the new API in general, which tries to move towards declarative rules rather than giving extensions access to web requests in-flight.
To start with, add the following to your extension manifest:
Copyright 2023 Google LLC.
SPDX-License-Identifier: Apache-2.0
"declarative_net_request" : {
"rule_resources" : [{
"id": "ruleset_1",
"enabled": true,
"path": "rules_1.json"
}]
},
"permissions": [
"declarativeNetRequest"
],
"host_permissions": [
"https://example.com/*"
]
This defines a new "ruleset" and also adds the required API and host permissions.
You can then create the file rules_1.json and put the desired rule inside:
Copyright 2023 Google LLC.
SPDX-License-Identifier: Apache-2.0
[
{
"id" : 1,
"priority": 1,
"action": {
"type": "modifyHeaders",
"responseHeaders": [
{ "header": "Access-Control-Allow-Origin", "operation": "set", "value": "*" },
{ "header": "Access-Control-Allow-Methods", "operation": "set", "value": "GET, PUT, POST, DELETE, HEAD, OPTIONS" }
]
},
"condition" : {
"urlFilter": "||example.com"
}
}
]
This adds the two headers as required. You can then reload your extension and should be good to go!
As a note, you won't see these headers reflected in Dev Tools. This is a bug which will hopefully be fixed in the future: https://bugs.chromium.org/p/chromium/issues/detail?id=1247400
If you want to check it's working, going to https://example.com and running the following in the console will output a list of headers, including the ones added by your extension: [...(await fetch("https://example.com")).headers].

Can't access any file in subdirectories with Firebase Hosting

I have a site hosted in Firebase Hosting and it works fine when I just load content from the current public folder. But I can't load anything from a subdir like /img/logo.png.
I've searched here and over the internet, but found no working solution. Some similar questions I tried:
Images not showing up in hosted site
How to include subdirectories in firebase hosting
My includes are like <script src="js/index.js"></script>
My firebase.json:
{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"headers": [ {
"source" : "**/*.#(eot|otf|ttf|ttc|woff|font.css)",
"headers" : [ {
"key" : "Access-Control-Allow-Origin",
"value" : "*"
} ]
}, {
"source" : "**/*.#(jpg|jpeg|gif|png)",
"headers" : [ {
"key" : "Cache-Control",
"value" : "max-age=7200"
} ]
}, {
"source" : "404.html",
"headers" : [ {
"key" : "Cache-Control",
"value" : "max-age=300"
} ]
} ]
}
}
Solved it by running firebase deploy inside Bash on Ubuntu on Windows.
Previously, I was using firebase cli on plain Windows 10.

Fiware: No observation attributes in Orion CB when registered/sent via IDAS UltraLight

This question is very similar to Missing attributes on Orion CB Entity when registering device through IDAS but found no definitive answer there.
I have been trying FiWare to get UL2.0 via IDAS to the Orion CB working in the Fiware-Lab env:
using latest GitHub
https://github.com/telefonicaid/fiware-figway/tree/master/python-IDAS4
scripts
following the tutorials in particular
http://www.slideshare.net/FI-WARE/fiware-iotidasintroul20v2
I have a FI-WARE Lab account with token generated. Adapted the config.ini file:
[user]
# Please, configure here your username at FIWARE Cloud and a valid Oauth2.0 TOKEN for your user (you can use get_token.py to obtain a valid TOKEN).
username=MY_USERNAME
token=MY_TOKEN
[contextbroker]
host=130.206.80.40
port=1026
OAuth=no
# Here you need to specify the ContextBroker database you are querying.
# Leave it blank if you want the general database or the IDAS service if you are looking for IoT devices connected by you.
# fiware_service=
fiware_service=bus_auto
fiware-service-path=/
[idas]
host=130.206.80.40
adminport=5371
ul20port=5371
OAuth=no
# Here you need to configure the IDAS service your devices will be sending data to.
# By default the OpenIoT service is provided.
# fiware-service=fiwareiot
fiware-service=bus_auto
fiware-service-path=/
#apikey=4jggokgpepnvsb2uv4s40d59ov
apikey=4jggokgpepnvsb2uv4s40d59ov
[local]
#Choose here your System type. Examples: RaspberryPI, MACOSX, Linux, ...
host_type=MACOSX
# Here please add a unique identifier for you. Suggestion: the 3 lower hexa bytes of your Ethernet MAC. E.g. 79:ed:af
# Also you may use your e-mail address.
host_id=a0:11:00
I used the SENSOR_TEMP template, adding the 'protocol' field (PDI-IoTA-UltraLight which as the first problem I stumbled upon):
{
"devices": [
{ "device_id": "DEV_ID",
"entity_name": "ENTITY_ID",
"entity_type": "thing",
"protocol": "PDI-IoTA-UltraLight",
"timezone": "Europe/Amsterdam",
"attributes": [
{ "object_id": "otemp",
"name": "temperature",
"type": "int"
} ],
"static_attributes": [
{ "name": "att_name",
"type": "string",
"value": "value"
}
]
}
]
}
Now I can Register the device ok. Like
python RegisterDevice.py SENSOR_TEMP NexusPro Temp-Otterlo
and see it in Device List:
python ListDevices.py
I can send Observations like
python SendObservation.py Temp-Otterlo 'otemp|17'
But in the ContextBroker I see the Entity but never the measurements, e.g.
python GetEntity.py Temp-Otterlo
Gives
* Asking to http://130.206.80.40:1026/ngsi10/queryContext
* Headers: {'Fiware-Service': 'bus_auto', 'content-type': 'application/json', 'accept': 'application/json', 'X-Auth-Token': 'NULL'}
* Sending PAYLOAD:
{
"entities": [
{
"type": "",
"id": "Temp-Otterlo",
"isPattern": "false"
}
],
"attributes": []
}
...
* Status Code: 200
* Response:
{
"contextResponses" : [
{
"contextElement" : {
"type" : "thing",
"isPattern" : "false",
"id" : "Temp-Otterlo",
"attributes" : [
{
"name" : "TimeInstant",
"type" : "ISO8601",
"value" : "2015-10-03T14:04:44.663133Z"
},
{
"name" : "att_name",
"type" : "string",
"value" : "value",
"metadatas" : [
{
"name" : "TimeInstant",
"type" : "ISO8601",
"value" : "2015-10-03T14:04:44.663500Z"
}
]
}
]
},
"statusCode" : {
"code" : "200",
"reasonPhrase" : "OK"
}
}
]
}
I get an TimeInstant attribute strangely. I tried playing with settings of the .ini like fiware-service=fiwareiot, but to no avail. I am out of ideas. The documentation at the catalogue. for IDAS4
is talking about observations to be sent to port 8002 and setting "OpenIoT" service, but that failed as well.
Any help appreciated.
You should run "python SendObservation.py NexusPro 'otemp|17'" instead of "python SendObservation.py Temp-Otterlo 'otemp|17'".
The reason is that you are providing an observation at the southbound and then, the DEV_ID should be used.
The entity does not include an attribute until an observation is received so then it is normal you are not able to see it. Once you try the one above it should all work.
Cheers,

Error with the JSON File while using scrapy for crawling

I am using a script, Financial-News-Crawler, that I found on GitHub using Scrapy. Basically this crawls the mentioned website. It uses a Json file where rules and paths are mentioned for the website to crawl. I had a look at the scrapy documentation in order to write the JSON file for my yahoo finance website. When I try to run the script it says "No json could be decoded."
Here is the structure of the .json file:
{
"allowed_domains" : [“finance.yahoo.com”],
"start_urls": [
"http://finance.yahoo.com/investing-news/"
],
"rules": [
{
"allow": [“/investing-news"],
"follow": true
},
],
"paths": {
"title" : ["//title/text()"],
"date" : ["//span[#class='datestamp']/text()"],
"text" : ["//div[#id=‘article_content’]”, "//div[#id='article_body']"]
},
"source": “finance yahoo“,
"company": “Yahoo”
}
What am I doing anything wrong?