This is my first time experimenting with service workers, and I'm sure I'm doing something stupid.
I have a service worker set up for a WordPress theme I'm working on. It's located at /public_html/wp-content/themes/framework/assets/scripts/service-worker.js.
I have set the header Servie-Worker-Allowed: "/" via the .htaccess file.
I'm using sw-toolbox to make things easier. My script is below.
Script:
toolbox.precache(["/", "../media/logo.svg", "../media/spritesheet.svg", "../scripts/modern.js", "../styles/modern.css"]);
toolbox.router.get("../media/*", toolbox.cacheFirst);
toolbox.router.get("/wp-content/uploads/*", toolbox.cacheFirst);
toolbox.router.get("/*", toolbox.networkFirst, {NetworkTimeoutSeconds: 5});
The service worker properly registers, and no errors are thrown. All files set to precache show up under Cache > Cache Storage in Chrome's developer tools correctly. For some reason these cached files aren't being served when offline.
I know there's issues with the scope of the service worker, but the Service-Worker-Allowed header should correct for that. Given that the files do in fact show up in cache without issue, I'd think that this is all working correctly.
What am I missing?
Note: I'd like to keep service-worker.js and the files I'm caching where they are and with relative paths; it becomes problematic moving them to the root or giving them absolute paths because this WordPress theme gets re-used on builds and has its name changed every time, making absolute paths a pain. I tested out rewriting to the root with .htaccess, which did work, but had it's own issues. I don't understand why that would work but what I'm currently trying wouldn't.
I think I was going about this wrong. There doesn't appear to be a need to manually specify to cache my theme assets as long as I've enabled caching in general. To that end, I've instead set up a rewrite rule so that service-worker.js lives at the root (i.e. https://www.example.com/service-worker.js), thus giving it correct scope. This has enabled my project to work offline. Code for this is below.
((global) => {
// disable the service worker for post previews
global.addEventListener("fetch", (event) => {
if (event.request.url.match(/preview=true/)) {
return;
}
});
// ensure the service worker takes over as soon as possible
global.addEventListener("install", event => event.waitUntil(global.skipWaiting()));
global.addEventListener("activate", event => event.waitUntil(global.clients.claim()));
// set up the cache
global.toolbox.precache(["/", "/offline/"]);
global.toolbox.router.get("/wp-content/uploads/*", toolbox.cacheFirst);
global.toolbox.router.get("/*", toolbox.networkFirst, {NetworkTimeoutSeconds: 5});
// redirect offline queries to offline page
self.toolbox.router.get("/(.*)", function (req, vals, opts) {
return toolbox.networkFirst(req, vals, opts).catch((error) => {
if (req.method === "GET" && req.headers.get("accept").includes("text/html")) {
return toolbox.cacheOnly(new Request("/offline/"), vals, opts);
}
throw error;
});
});
})(self);
Related
I got a simple Next app where I'm making an external API call to fetch some data. This worked perfectly fine until a couple days ago - when the app is making an API request, I can see in the network tab that the URL that it's trying to call, got Next app's address (localhost:3000) prepended in front of the actual URL that needs to be called e.g.: instead of http://{serverAddress}/api/articles it is calling http://localhost:3000/{serverAddress}/api/articles and this request resolves into 404 Not Found.
To make the API call, I'm using fetch. Before making the request, I've logged the URL that was passed into fetch and it was correct URL that I need. I also confirmed my API is working as expected by making the request to the expected URL using Postman.
I haven't tried using other library like axios to make this request because simply it doesn't make sense considering my app was working perfectly fine only using fetch so I want to understand why is this happening for my future experience.
I haven't made any code changes since my app was working, however, I was Dockerizing my services so I installed Docker and WSL2 with Ubuntu. I was deploying those containers on another machine, now both, the API I'm calling and Next app are running on my development machine directly when this issue is happening.
I saw this post, I confirmed I don't have any whitespaces in the URL, however, as one comment mentions, I installed WSL2, however, I am not running the app via WSL terminal. Also, I've tried executing wsl --shutdown to see if that helps, unfortunately the issue still persists. If this is the cause of the issue, how can I fix it? Uninstall WSL2? If not, what might be another possible cause for the issue?
Thanks in advance.
EDIT:
The code I'm using to call fetch:
fetcher.js
export const fetcher = (path, options) =>
fetch(`${process.env.NEXT_PUBLIC_API_URL}${path}`, options)
.then(res => res.json());
useArticles.js
import { useSWRInfinite } from 'swr';
import { fetcher } from '../../utils/fetcher';
const getKey = (pageIndex, previousPageData, pageSize) => {
if (previousPageData && !previousPageData.length) return null;
return `/api/articles?page=${pageIndex}&limit=${pageSize}`;
};
export default function useArticles(pageSize) {
const { data, error, isValidating, size, setSize } = useSWRInfinite(
(pageIndex, previousPageData) =>
getKey(pageIndex, previousPageData, pageSize),
fetcher
);
return {
data,
error,
isValidating,
size,
setSize
};
}
You might be missing protocol (http/https) in your API call. Fetch by default calls the host server URL unless you provide the protocol name.
Either put it into env variable:
NEXT_PUBLIC_API_URL=http://server_address
Or prefix your fetch call with the protocol name:
fetch(`http://${process.env.NEXT_PUBLIC_API_URL}${path}`, options)
I need some help with Cloudflare workers. I'm not a dev by any stretch of the imagination and use Webflow so no code, but I've got a Webflow project where I need to squeeze every ounce of speed out of the site and I'm trying it on my domain first.
I've got a Pro Cloudflare account set up for the project, but my problem is that Webflow keeps all the assets like css, fonts and most importantly images on assets.website-files.com (their CDN) and not my actual domain, so Cloudflare is not caching or applying any performance enhancements on the images, etc... as assets.website-files.com is not part of my domain as you can see from the screenshot:
Dr.Falre
I contacted Cloudflare and they sent me this Cloudflare Workers script, but like I said, no code experience here, so would anyone here be able to help me out with this script from Cloudflare:
/**
* An object with different URLs to fetch
* #param {Object} ORIGINS
*/
const ORIGINS = {
"starwarsapi.yourdomain.com": "swapi.dev",
"google.yourdomain.com": "www.google.com",
}
async function handleRequest(request) {
const url = new URL(request.url)
// Check if incoming hostname is a key in the ORIGINS object
if (url.hostname in ORIGINS) {
const target = ORIGINS[url.hostname]
url.hostname = target
// If it is, proxy request to that third party origin
return await fetch(url.toString(), request)
}
// Otherwise, process request as normal
return await fetch(request)
}
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
Any help would be much appreciated.
I have a problem that has been chasing me for the last few days.
I did all the necessary configuration to set up the Google Play Games sign-in method to my Unity game for Android.
However, every time I click the sign-in button the Play Games icon briefly appears at the top and the app suddenly crashes.
Analyzing the logcat, it seems there is an error on the highlighted line (last line of the 'InitializePlayGamesPlatform()' method) of the following script:
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine;
public class UserManager : MonoBehaviour
{
private Firebase.Auth.FirebaseAuth firebaseAuth;
private Firebase.Auth.FirebaseUser firebaseUser;
void Start()
{
InitializePlayGamesPlatform();
}
private void InitializePlayGamesPlatform()
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.RequestServerAuthCode(false)
.Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();
************firebaseAuth = Firebase.Auth.FirebaseAuth.DefaultInstance;************
}
public void TrySignIn()
{
UnityEngine.Social.localUser.Authenticate((bool success) =>
{
if (!success)
{
Debug.LogError("UserManager: Failed to sign in into Play Games!");
return;
}
string authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
if (string.IsNullOrEmpty(authCode))
{
Debug.LogError("UserManager: Failed to get auth code!");
return;
}
Debug.LogFormat("UserManager: auth code is: {0}", authCode);
Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode);
firebaseAuth.SignInWithCredentialAsync(credential).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("UserManager: sign-in was canceled!");
return;
}
if (task.IsFaulted)
{
Debug.LogError("UserManager: error signing in!");
return;
}
firebaseUser = task.Result;
Debug.LogFormat("UserManager: user signed in successfully: {0} ({1})", firebaseUser.DisplayName, firebaseUser.UserId);
});
});
}
public void TrySignOut()
{
firebaseAuth.SignOut();
}
}
Also, there are some other messages/errors in the logcat, such as:
Failed to read Firebase options from the app's resources. Either make sure google-services.json is included in your build or specify options explicitly.
InitializationException: Firebase app creation failed.
at Firebase.FirebaseApp.CreateAndTrack (Firebase.FirebaseApp+CreateDelegate createDelegate, Firebase.FirebaseApp existingProxy) [0x000da] in Z:\tmp\tmp.txs8ldQ514\firebase\app\client\unity\proxy
at Firebase.FirebaseApp.Create () [0x00000] in Z:\tmp\tmp.txs8ldQ514\firebase\app\client\unity\proxy\FirebaseApp.cs:119
at Firebase.FirebaseApp.get_DefaultInstance () [0x0000b] in Z:\tmp\tmp.txs8ldQ514\firebase\app\client\unity\proxy\FirebaseApp.cs:94
at Firebase.Auth.FirebaseAuth.get_DefaultInstance () [0x00000] in Z:\tmp\tmp.poUq23PLco\firebase\auth\client\unity\proxy\FirebaseAuth.cs:294
I repeated every procedure from the beginning a thousand times, searched everywhere and got no results.
I have tried running an older version of GPG plugin as well, but no success at all.
I kindly ask you to help me on this - I promise to put your names on the credits in case I publish!! :)
Thanks in advance!!
A Firebase support member (#Jesus) has just helped me to figure the issue out. The workaround is directly adding the sourceset to the mainTemplate.gradle.
I had to do the following:
Go to Project Settings > Publishing Settings > Build > checkmark Custom Main Gradle Template.
It will give you the location to the mainTemplate.gradle file.
Here is an example.
Go to the given directory and open mainTemplate.gradle with a text editor.
Add the following code to the referred file, right after lintOptions:
sourceSets { main { res.srcDirs += '<full path from your root directory to google-services.xml>' } }
Also, add this array to noCompress under aaptOptions:
noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'google-services.json']
In the end, the mainTemplate.gradle should look like this:
lintOptions {
abortOnError false
}
sourceSets { main { res.srcDirs += 'C:\\Users\\USERNAME\\Documents\\Unity Projects\\GAMENAME\\Assets\\Plugins\\Android\\Firebase\\res\\values\\google-services.xml' } }
aaptOptions {
noCompress = ['.unity3d', '.ress', '.resource', '.obb', 'google-services.json']
ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
}
in my case i have setted prograurd mode in release setting under publish settings>minify>release . Setting this to none have fixed my issue.
That particular issue is related to the google-services.json file that should be automatically included in your build if the file exists in your Assets/ directory. Note that the process is different enough from the Android process that you probably need to ignore the Firebase Android instructions and only follow the Unity ones unless you really know what you're doing.
I'll tackle the easiest problem first. Make sure you have your google-services.json file in your Assets/ directory. I usually keep mine in Assets/Data/ (which is where I also keep things like ScriptableObjects). If you don't have it, follow the instructions on the Download Firebase config file or object help page:
Get config file for your Android app Go to your the Settings icon
Project settings in the Firebase console. In the Your apps card,
select the package name of the app for which you need a config file.
Click google-services.json. Move your config file into the module
(app-level) directory of your app. Make sure that you only have this
most recent downloaded config file in your app.
Once you have that file, you'll need to make sure that Assets/Plugins/Android/Firebase/res/values/google-services.xml gets properly generated:
The reason why this exists is that the Unity SDK does not use the Google Services Gradle Plugin - doing so would break compatibility with older versions of Unity or teams that otherwise opt out of gradle integration. This is why I strongly advise against following any of the Android specific documentation unless you're willing to do a lot of work by hand.
If you do not have google-services.xml or you want to try to regenerate it, you should navigate to Assets>External Dependency Manager>Android Resolver>Force Resolve:
If this does fix your issue, then you should make sure that you enable auto-resolution to avoid this issue in the future:
There are also a few more subtle issues that crop up. One is that "External Dependency Manager for Unity" is a rename of the "Play Services Resolver" (developers kept thinking that it was tied to Play Services - which it isn't). So check that you don't have a Assets/Play Services Resolver directory. Also, if you're using the External Dependency Manager in the Unity Package Manager, make sure that Assets/External Dependency Manager doesn't exist as well. Since you're using two plugins that ship with EDM4U, you may have some duplication (although EDM4U should be smart enough to resolve that).
If you're still running into issues, it may help to share your directory layout WRT where the External Dependency Manager, google-services.json, google-services.xml, and the Firebase and the Play Games Sign-In plugin live. Also it could be worth noting if google-services.xml makes it into an exported project (or your APK). If any of this isn't something you want to share on Stack Overflow, feel free to reach out to Firebase Support and link this SO question.
--Patrick
I have a site similar to Stackoverflow where users can create a post (or question) which gets its own URL and should be SEO optimized. Therefore I need to include these dynamic pages in my SiteMap.xml. I would like to find an automatic way to insert each dynamic URL to my Sitemap when initially created.
Hoping to not reinvent the wheel, I found sitemap-module for nuxt, however the example they use for dynamic pages is statically written, so not sure what good that does.
I am having a hard time even conceptualizing how to set this up and what is possible with current infrastructure. Can Firestore functions update source code and redeploy or are there any firestore hosting features to help? Could/ should I set up a cron job to run every night to first run a script to query firestore and update sitemap file on local computer, then automatically deploy it to firestore from command line? Any script examples?
Tech used: VueJS, Node.js, Nuxt/ SSR, Firestore (db and hosting), and Express
This is how I did it. Hope this helps. Please share if you managed to get a different solution.
I used npm install #nuxtjs/sitemap
Website here - #nuxtjs/sitemap
In nuxt.config.js
var routes = []
var allUsers = [{'username': 'username'}] // Getting users as an Array
for (var i = 0; i < allUsers.length; i++) {
routeObject = {
'url': '/profile/' + allUsers[i].username
}
routes.push(routeObject);
}
module.exports = {
sitemap: {
path: '/sitemap.xml',
hostname: 'Your hostname here',
cacheTime: 1000 * 60 * 15,
gzip: true,
generate: false,
routes: routes
}
}
I want to verify that I am placing my Iron Router code in the correct location. Right now I have it stored in lib/router.js, which means the code is shared on the client and server. Is that correct?
Also, some of my routes require admin status, such as:
Router.route('/manage', function () {
if ($.inArray('admin', Meteor.user().roles) > -1) {
this.render('manage');
} else {
this.render('403_forbidden');
}
});
Is that code safe in its current location? I am also interested in knowing how I can test these kinds of security holes so that I don't have to ask in the future.
Thanks
As to location of router.js ...
Yes, you want it to be available on both the client and server. So putting inside the /lib directory is fine. Really, you can put it anywhere other the the /client or /server directories.
FWIW, in most projects I've looked at, router.js is stored in the top-level project directory. Possibly this is to avoid load order issues (i.e. if the router has some dependencies on files in /lib, /client, or /server, which will generally be loaded before top-level files), or possibly its because everyone I've looked at is working off the same boilerplate code. Check out the Meteor official docs if you want to know more about load order.
As for your admin question, that route should be OK. You can test it by opening up a client side console like firebug and trying something like :
Meteor.users.update(Meteor.userId(), {$set: {roles: ['admin']}});
I believe users can only update fields in the Meteor.users.profile, so this should fail. If it doesn't, you can just add the following deny rule (in client + server);
Meteor.users.deny({
update: function() {
return true;
}
});