Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

manifest.json - Set PWA start_url in web app manifest based on the URL that was added to homescreen

I would like to make the start_url in the manifest at the where user visit the landing page. https://example.com/1234 https://example.com/4321

this is my php to fetch whole url {{ Request::fullUrl() }} I put at "start_url" : "{{ Request::fullUrl() }}", it doesn't fetch in json file.

I had try change the manifest.json to manifest.php, but don't know how to convert the json content into php to make it work.

<link rel="manifest" href="/_manifest.php?start_url=val" data-pwa-version="set_in_manifest_and_pwa_js">

I know this is old question and I search all over the internet still can't find the solution. Set start_url in web app manifest based on the URL that was added to homescreen this doesn't have a solution. delete the start_url doesn't make the PWA work.

Any expert could help me? Appreciate! I'm newbie on coding, please forgive my limited knowledge.

question from:https://stackoverflow.com/questions/65948808/set-pwa-start-url-in-web-app-manifest-based-on-the-url-that-was-added-to-homescr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Chrome (and most other browsers) require you provide the start_url in the manifest, and it can't be dynamic.

You could build this behaviour by saving the page the user was on when the PWA was installed. Then, set the start_url to a simple redirect page that checks to see what page the user was on when they installed, and redirect them to that page.

To save the page the user is on when the PWA is installed, listen for the appinstalled event, then save the page to localStorage:

window.addEventListener('appinstalled', () => {
  localStorage['installedFrom'] = window.location.pathname;
});

Then, in the redirect page, do something like:

const installedFrom = localStorage['installedFrom'];
if (installedFrom) {
  window.location.replace(installedFrom);
} else {
  // No install page saved, send them to the home page?
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...