Ionic 4 - How to configure a PWA to run on a subfolder? - progressive-web-apps

There are enough tutorials on how to create a PWA using ionic 4 but all the solutions I found are focused on deploying the application in the root.
What I need is to deploy my entire application in a subfolder:
https://my-domain.com/my-app/
I explained in detail what's my problem in this repository:
https://github.com/Viterbo/ionic-4-PWA
Thanks

This doesn't address the Ionic 4 aspect of this, but I figured it out how to get a PWA to install from a subfolder, the answer is here: https://www.w3.org/TR/appmanifest/#navigation-scope:
"If the scope member is not present in the manifest, it defaults to the parent path of the start_url member. For example, if start_url is /pages/welcome.html, and scope is missing, the navigation scope will be /pages/ on the same origin. If start_url is /pages/ (the trailing slash is important!), the navigation scope will be /pages/.
So, to run by itself (root folder), the start_url looks like this:
"start_url": "/index.html?utm_source=homescreen",
but when serving the PWA from a subfolder, it looks like this:
"start_url": "/app-folder/index.html?utm_source=homescreen",
"scope": "/app-folder/",
I tested this on the latest Chrome on Windows 10 and the latest Android 9 release.

Here's ewhat worked for me, First scope and start url should be same
manifest.json
"scope": "/admin/",
"start_url": "/admin/?u=what-ever"
service worker path should be same as scope
index.html
function onLoad() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/admin/sw.js')
.then(function (reg) {
console.log("Yes");
}).catch(function (err) {
console.log("No", err)
});
}
}
window.addEventListener('load', onLoad);

Related

Flutter web can't load network image from another domain

I can't load network images in flutter web from other domains with API calls. getting this error
Trying to load an image from another domain? Find answers at:
https://flutter.dev/docs/development/platform-integration/web-images
ImageCodecException: Failed to load network image.
any help?
For being able to display your images from any other Domain or from Firebase Storage on a Flutter web page you have to configure your data for CORS.
Open the GCP console, select your project and start a cloud terminal session by clicking the >_ icon button in the top navbar.
Click the open editor button (pencil icon), then create the cors.json file.
The cors.json file should look like this:
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
I set the origin to * which means that every website can display your images. But you can also insert the domain of your website there to restrict access.
Run gsutil cors set cors.json gs://your-bucket
If you need more information: https://cloud.google.com/storage/docs/configuring-cors
There are two ways to resolve this either run your app using HTML renderer or set up the CORS configuration.
1. Using HTML renderer
Taken from the docs
CORS is a mechanism that browsers use to control how one site accesses the resources of another site. It is designed such that, by default, one web-site is not allowed to make HTTP requests to another site using XHR or fetch. This prevents scripts on another site from acting on behalf of the user and from gaining access to another site’s resources without permission
When using <img>, <picture>, or <canvas>, the browser automatically blocks access to pixels when it knows that an image is coming from another site and the CORS policy disallows access to data.
Flutter has two renderers for web, canvaskit and html
When you run/build app on the flutter web it uses renderers based on the device where its running.
HTML renderer: when the app is running in a mobile browser.
CanvasKit renderer: when the app is running in a desktop browser.
auto (default) - automatically chooses which renderer to use.
The HTML renderer can load cross-origin images without extra configuration.
so you could use these commands to run and build the app.
flutter run -d chrome --web-renderer html // to run the app
flutter build web --web-renderer html --release // to generate a production build
source: https://docs.flutter.dev/development/tools/web-renderers
2. Setup CORS Configuration
Download the Google-cloud-sdk which contains a tool called gsutil
In your flutter project create a file called cors.json and add this json file which will remove all domain restrictions.
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
Run gcloud init (located in google-cloud-sdk/bin)
Authenticate yourself by clicking the link and choose the project in the console.
finally execute gsutil cors set cors.json gs://<your-bucket-name>.appspot.com You can find your bucket name in firebase storage.
I have documented this entire process in github gists
i solve this issue by using html renderer
flutter build web --release --web-renderer html
or
flutter run --web-renderer html
for me flutter run -d chrome --web-renderer html worked.
Simply.. in your flutter (web/index.html) add this:
If you use firebase storage just follow these steps:
Open Google Cloud Console at your project
Click on console icon in top right corner
Click Open editor
Click File->New->cors.json
Place code below
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
Then Run in console
**
gsutil cors set cors.json gs://bucket-name
**
bucket-name is the name of the storage bucket which you can find on your firebase project above the folders in the storage section
Add this code in your web/index.html file
<script type="text/javascript">
window.flutterWebRenderer = "html";
</script>
To debug quickly, instead of flutter run -d chrome --web-renderer html running from the terminal you can also add the arguments --web-renderer html on your run config. On the menu bar, navigate through Run > Edit Configurations
If you can't update CORS settings or add proxy, prefer CanvasKit (has better performance) over HTML renderer - could display image with platform view:
import 'dart:html';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
class MyImage extends StatelessWidget {
#override
Widget build(BuildContext context) {
String imageUrl = "image_url";
// https://github.com/flutter/flutter/issues/41563
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
imageUrl,
(int _) => ImageElement()..src = imageUrl,
);
return HtmlElementView(
viewType: imageUrl,
);
}
}
just add cors.json in web folder
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
After creating cors.json and running the commands, nothing worked!
Finally what worked was adding a script into index.html inside your web folder.
<script type="text/javascript">window.flutterWebRenderer = "html";</script>
Reference Image:
it seems it solved my issue by running flutter run --web-renderer html
This official solution worked for me on Chrome only (Source). But I had to run it first every time.
flutter run -d chrome --web-renderer html
And disabling web security also worked (Source). But the browsers will show a warning banner.
But In case you are running on a different browser than Chrome (e.g. Edge) and you want to keep 'web security' enabled.
You can change the default web renderer in settings in VS Code
File ==> Preferences ==> Settings ==> Enter 'Flutter Web' in the Search Bar ==> Set the default web renderer to html
For someone who uses Slim Framework, just create a .htaccess file on that folder for storing images and put this line of code
Header set Access-Control-Allow-Origin *
There is a two way to solve this issue :
1- Just run your flutter web with flutter run -d chrome --web-renderer html
But there is one problem when you renderer your canvas view to HTML view. So your all views like images, text, etc gone blurry(bad quality). If you sacrifice the quality so go on with the first solution.
If you don't sacrifice quality so you need to add some code to your backend site I have done with node js you can use with yours.
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
So, I chose the second solution because I don't sacrifice quality. So I provide you with my backend screenshot for better understanding hope you found your solution.
I had an issue while loading content from other domains which I don't have control over to change the cors settings from the server-side. I have found a work around for this problem.
STEP 1: go to C:\src\flutter\packages\flutter_tools\lib\src\web or navigate from your flutter root directory to flutter\packages\flutter_tools\lib\src\web.
STEP 2: open chrome.dart in your text editor.
STEP 3: add '--disable-web-security' under the like '--disable-extensions' as save the file.
STEP 4: run flutter clean in your project folder and run the app.
Hope this works for you.
I haven't tested my application in production yet. Adding this flag might also cause some security issues.
The Ultimate Solution
Use this package instead of flutter's NetworkImage.
https://pub.dev/packages/image_network
Tested on Flutter web with Canvas renderer and it works like a charm!
Verified answer is correct, Upvoted you man. Here's how I did it

How to transform Flutter web app into a PWA?

I am making a new Web application with Flutter web.
I want to add service workers to my app in order to make it a PWA.
What I need to use to achieve my goal ?
I have tried to do it with dart packages (service_worker or pwa), but they are deprecated for Dart 2.
Since a service worker is just javascript code you can write one in plain javascript (e.g. in a sw.js file), bundle it with your flutter web app and then register the service worker from the index.html file like this (source):
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js');
}
You currently need to switch to Flutter Channel Dev (at Feb 17th 2020, current Dev version 1.15.3)
For existing projects, use the terminal to hit the command:
flutter create .
For new projects, you would have a 2 files: manifest.json and index.html inside your web folder.
While checking index.html you would notice the following script:
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('/flutter_service_worker.js');
});
}
If the script is not there, added inside <body></body>
This script creates the PWA support for you and be able to "install" the app on a device (this also includes MacOS)
Follow this GUIDE to complete your PWA functionalities.

SAPUI5 app cannot load the CSS file in ABAP repository

I have one SAPUI5 application.
I added one custom css file to it via manifest.json like this:
"resources": {
"css": [
{
"uri": "css/style.css"
}
]
},
While this file can be loaded correctly in WEB IDE when I deploy it to ABAP repository its address does not resolved correctly. Actually it try to load the CSS file with the same address that it uses in WEB IDE and obviously it will fail.
What can cause this issue?
I found the reason. I used the manifest version 4.0, and in this version the app id must be the same as the namespace otherwise it cannot resolved the addresses correctly. By making the appId and namespace equal everything going well.

Fiori launchpad fails to load UI5 app because it cannot find registered custom module

I've a custom control in my app. It runs as a standalone web application fine. But when I'm launching it from the Fiori Launchpad (FLP), it logs an error in the console. I've registered my control in my index.html file:
<script>
sap.ui.localResources("sap.custom");
sap.ui.localResources("sap.ui.codetools");
sap.ui.localResources("libs");
</script>
The structure of file directory is:
If I remove my custom control, then I can run my app in the launchpad. Do I need to add some settings in my manifest file? What could be the reason for this error?
The common configuration for a Fiori tile is to point to the Component file. So the index.html file is not deployed together with the app. If it's absolutely necessary to register additional module paths, do it in manifest.json instead.
{
"sap.ui5": {
"resourceRoots": {
"sap.ui.codetools": "sap/ui/codetools"
}
}
}
Documentation: Descriptor for Applications, Components, and Libraries (See resourceRoots).
The common configuration for a Fiori tile is to point to the Compopent.js file. So the index.html is not called. The FioriLaunchpad.html plays the role of the index.html and the ComponentContainer is defined there.
Try to register your custom control somewhere else within the app.
Activate the APP Node on SICF( node Name is same as App)

Chrome App not launching when published

I've created a Chrome app which, when I load locally works fine. However, when I publish it to the Chrome App store and install I get a 404 that it couldn't find index.html. Really lost at this point. Here's the app section of my manifest.json:
"app": {
"launch": {
"local_path": "index.html"
}
},
Check the unpacked version of the Chrome app in the Extensions directory
[User Data Directory]/[Profile Dir (likely named 'Default')]/Extensions/aaaaaaaaaaaahashkeyzzzzzzzzzz
Try
Loading an unpackaged app from that installed directory.
Examining the files, is index.html missing, or file permissions set incorrectly?