Skip to content

[Website] Fix Safari failing to import main module after deployments#3215

Merged
adamziel merged 1 commit intotrunkfrom
fix-safari-module-import-retry
Jan 28, 2026
Merged

[Website] Fix Safari failing to import main module after deployments#3215
adamziel merged 1 commit intotrunkfrom
fix-safari-module-import-retry

Conversation

@adamziel
Copy link
Copy Markdown
Collaborator

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:

  • No network request is initiated
  • Service Worker fetch handler is not called
  • Error provides no useful debugging information
  • Issue occurs specifically after new deployments
  • CORS is configured correctly
  • Not related to Safari's memory cache (no fetch attempts in Network tab)

The Solution

Catch the import error and implement a retry mechanism:

  1. Fetch the module URL explicitly to prime the cache/service worker
  2. Reload the page with a ?_ts=<timestamp> parameter to bust cache
  3. Extract the actual built chunk URL by stringifying a function containing the import (allows Vite to transform it)
  4. Clean up the URL after successful retry using the history API
  5. Prevent infinite loops by only retrying once

Interestingly, 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

  • Add error handling around import('./src/main')
  • Extract module URL using function stringification technique (Vite transforms the import in the function definition)
  • Fetch module with cache: 'no-store' to prime cache
  • Reload with timestamp parameter on failure
  • Remove timestamp parameter after successful retry

Test plan

  • Deploy to staging and test in Safari after deployment
  • Verify module loads successfully on first try (normal case)
  • Verify retry mechanism works when import fails
  • Verify URL cleanup after successful retry
  • Verify no infinite reload loops occur
  • Test on various Safari versions (macOS and iOS)

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 adamziel changed the title Fix Safari failing to import main module after deployments [Website] Fix Safari failing to import main module after deployments Jan 28, 2026
@adamziel adamziel merged commit 7e20874 into trunk Jan 28, 2026
35 checks passed
@adamziel adamziel deleted the fix-safari-module-import-retry branch January 28, 2026 21:18
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.
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.

1 participant