[Website] Fix Safari failing to import main module after deployments#3215
Merged
[Website] Fix Safari failing to import main module after deployments#3215
Conversation
Safari sometimes fails with "TypeError: Importing a module script failed" when trying to import the main module after a new deployment. The error provides no stack trace or useful debugging information. After extensive debugging, the issue was traced to Safari not even attempting to fetch the module - no network request is made and the service worker fetch handler is not called. The root cause remains unclear, but it appears to be cache-related. The solution is to catch the import error, fetch the module URL explicitly to prime the cache, then reload the page with a timestamp parameter. This workaround successfully resolves the issue. After a successful retry, the timestamp parameter is removed from the URL using the history API. The actual module URL is extracted by stringifying a function containing the import statement, allowing Vite to transform it to the correct built chunk URL.
adamziel
added a commit
that referenced
this pull request
Jan 28, 2026
## Summary Follows up on #3125 in an attempt to finally resolve #3064. #3125 introduced a `fetch("main.js")` to bust the Safari cache and load that ES module after a page reload. That solution turned out to be, unfortunately, flaky. This PR retries to import `main.js` with a cache buster string, similarly to `import("main.js?_ts=178961653")`. It seems to have worked for me during testing. Fingers crossed it solves it once and for all! ## The Problem As documented in #3215, Safari sometimes fails with "TypeError: Importing a module script failed" when trying to import the main module after a new deployment. The previous solution (fetch + page reload) worked but was heavy-handed. ## Test plan 1. Open playground.wordpress.net in Safari on iOS 2. Plug it in to a Mac with a cable 3. Open devtools for that tab in Desktop Safari 4. Deploy this PR 5. Refresh the tab on iOS 7. Confirm the website reloaded correctly 8. If you also can see the "Failed to load main module" error in the console, this fix works. 9. If you can't, we don't know if it fully works yet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Safari sometimes fails with "TypeError: Importing a module script failed" when trying to import the main module after a new deployment. The error provides no stack trace, line numbers, or any useful debugging information.
After extensive debugging, the issue was traced to Safari not even attempting to fetch the module - no network request is made and the service worker fetch handler is not called. The root cause remains unclear, but it appears to be fundamentally cache-related.
The Problem
When
import('./src/main')runs in Safari:The Solution
Catch the import error and implement a retry mechanism:
?_ts=<timestamp>parameter to bust cacheInterestingly, triggering a
fetch()to the same URL and getting a 200 response resolves the issue after a page reload (but not before the page reload). This confirms the cache-related nature of the problem.Changes
import('./src/main')cache: 'no-store'to prime cacheTest plan