Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching and service worker docs to the web FAQ #11397

Merged
merged 5 commits into from
Nov 20, 2024

Conversation

johnpryan
Copy link
Contributor

This adds some information about web caching and service workers to the web FAQ.

cc/ @ditman @kevmoo

This adds some information about web caching and service workers to the web FAQ.

cc/ @ditman @kevmoo
@johnpryan johnpryan requested review from sfshaza2, parlough and a team as code owners November 14, 2024 18:35
@flutter-website-bot
Copy link
Collaborator

flutter-website-bot commented Nov 14, 2024

Visit the preview URL for this PR (updated for commit 50360f3):

https://flutter-docs-prod--pr11397-caching-and-service-workers-77emc0en.web.app

Copy link
Contributor

@sfshaza2 sfshaza2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few little things. I'll go ahead and LGTM, but please fix. :)

src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
src/content/platform-integration/web/faq.md Outdated Show resolved Hide resolved
@sfshaza2 sfshaza2 added the review.await-update Awaiting Updates after Edits label Nov 18, 2024
johnpryan and others added 2 commits November 19, 2024 09:37
Co-authored-by: Shams Zakhour (ignore Sfshaza) <[email protected]>
@sfshaza2 sfshaza2 merged commit b3b030b into main Nov 20, 2024
9 checks passed
@sfshaza2 sfshaza2 deleted the caching-and-service-workers-web-faq branch November 20, 2024 20:39
@ditman
Copy link
Member

ditman commented Nov 20, 2024

@slavap
Copy link

slavap commented Nov 20, 2024

@johnpryan @ditman
Unfortunately this section https://docs.flutter.dev/platform-integration/web/faq#how-do-i-clear-the-web-cache-after-a-deployment-and-force-an-app-download
does not provide a working solution and only adds confusion.
There are three ways to solve this:

  1. Properly configure cache headers on hosting - it is not so obvious and needs multiple examples.
  2. Use "subfolder version" approach - it is simple and works without any headers [web] DeferredLoadException after deploying a new version of app flutter#127459 (comment)
  3. Post-processing script for adding ?v=nnn for everything - it is kind of proper implementation of the mentioned section, i.e.
<script src="flutter_bootstrap.js?v=123" async></script>

is naïve, and has to be like bash script from flutter/flutter#127459 (comment)

@johnpryan
Copy link
Contributor Author

johnpryan commented Nov 21, 2024

Unfortunately this section does not provide a working solution and only adds confusion.

Do you think we should just remove it? This section is only necessary if you need to clear the web cache immediately after deploying a new version, which is probably not a requirement for most apps. If you have a link we can point to for better guidance I'd be happy to replace it.

Properly configure cache headers on hosting - it is not so obvious and needs multiple examples.

What would those additional examples look like? We are providing a Firebase example in the FAQ now.

Use "subfolder version" approach

This is an interesting approach - have you had any issues with it? Are you using a routing package like go_router?

Post-processing script for adding ?v=nnn for everything - it is kind of proper implementation of the mentioned section, i.e. is naïve, and has to be like bash script from flutter/flutter#127459 (comment)

I think it's a commonly used technique, but you're right it's not as simple as we describe in the docs. I'm happy to provide a link to a blog post or article that explains this technique.

@slavap
Copy link

slavap commented Nov 21, 2024

@johnpryan

Do you think we should just remove it?

Of course not! But this section is very important and must provide a complete solution for Flutter Web's caching problems.
Just search how many issues are opened about caching, deferred loading, pubspec version, web worker, ...
Developers are just lost and need a clear detailed explanation.

We are providing a Firebase example in the FAQ now.

Firebase is fine, but it has to be more "generic" - i.e. which headers for which files, and then how to implement this strategy on different hosting platforms (e.g. Firebase, AWS, Azure, self-hosted Apache, Tomcat, ...). Perhaps the community could help here.

This is an interesting approach - have you had any issues with it? Are you using a routing package like go_router?

For me, it works great. But I'm using auto_route and not bothering about # in urls.
The only reported problem was about using it with "path URL strategy", but even for that case there is a workaround flutter/flutter#127459 (comment)
So I believe this solution could be added to the docs, considering it does not require any cache headers config on hosting.
Actually, this trick is partially influenced by GWT, which compiles deferred scripts to a unique named subfolder on every build.

not as simple as we describe in the docs. I'm happy to provide a link to a blog post or article that explains this technique.

you may directly add this link to the docs flutter/flutter#127459 (comment)
OR just copy bash script from there to the docs.
The problem is this script patches compiled js results, which is scary for people, and also new versions of Flutter may require adjustments of this script. Ideally, it would be really nice if "flutter build web" just had a compile option for adding this "?v=nnn" in ALL proper places during the build.

@ditman
Copy link
Member

ditman commented Nov 21, 2024

For me, it works great. But I'm using auto_route and not bothering about # in urls.

@slavap How are you redirecting people from the root of your domain to your versioned folder deployment? That's the bit that I haven't seen solved!

@slavap
Copy link

slavap commented Nov 21, 2024

@ditman not sure I’ve got your question. index.html is placed exactly the same as usual Flutter Web deployment, only the rest of files go to subfolder on the same level as index.html

@ditman
Copy link
Member

ditman commented Nov 21, 2024

@ditman not sure I’ve got your question. index.html is placed exactly the same as usual Flutter Web deployment, only the rest of files go to subfolder on the same level as index.html

Ah, so you're updating your index.html to inject assets from /versionA to /versionB when you deploy? Understood! (I thought you were moving the whole build/web under a versioned folder)

@slavap
Copy link

slavap commented Nov 22, 2024

@ditman The main point is that there is no need to do "heavy" editing of index.html, it is usually just enough to change "base href" as:

<head>
  <base href="web1234/">

@philitell
Copy link

Please support documenting the "subfolder"-approach. This a simple workaround for a major problem that has been around way too long.
You just need to change base href e.g. by using the "--base-href" argument for flutter build and move the index.html file to the parent folder.

  1. Create a build
flutter build web --base-href "/my_project_1_1_2/"
  1. Copy content of build/web to a folder on your server (folder name contains the version number in one or other way) and move the index.html file to the parent folder. So the final file/folder-structure on your server looks like:
my_project/
   index.html
   my_project_1_1_2/

For Deployment of e.g. version 1.1.3 you deploy your files to subfolder "my_project_1_1_3" and update index.html in the parent directory.

@zs-dima
Copy link

zs-dima commented Dec 5, 2024

@philitell it is a very bad workaround

@philitell
Copy link

philitell commented Dec 5, 2024

@zs-dima This workaround achieves the desired behaviour and is very easy to implement. What do you consider a "good" workaround for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants