fix(frontend): stop swallowing Vite preload errors into router crashes - #2887
Draft
posthog-eu[bot] wants to merge 1 commit into
Draft
fix(frontend): stop swallowing Vite preload errors into router crashes#2887posthog-eu[bot] wants to merge 1 commit into
posthog-eu[bot] wants to merge 1 commit into
Conversation
The vite:preloadError handler called event.preventDefault(), which makes Vite's preload helper resolve with undefined instead of rethrowing. A lazy route then resolves to a falsy component and vue-router throws "Couldn't resolve component", a hard crash that also escapes the PostHog stale-asset suppression. Drop preventDefault() so the rejection surfaces as "Failed to fetch dynamically imported module" (already matched by STALE_ASSET_ERROR_PATTERNS), restoring both the auto-reload and the PostHog suppression. Add a router.onError fallback that treats "Couldn't resolve component" as a chunk error too, so a navigation that races the reload still recovers. Generated-By: PostHog Code Task-Id: 8387f1f6-97b6-447f-8ff4-d425a275e501
Contributor
Merging this PR will not alter performance
Comparing Footnotes
|
|
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
index.htmlis open can trigger it — any lazy route, not just the device page.vite:preloadErrorhandler insrc/main.tscalledevent.preventDefault(). Vite's preload helper only rethrows the import failure when!e.defaultPrevented, so preventing the default makes__vitePreloadresolve withundefined. The lazy route then resolves to a falsy component and vue-router throwsCouldn't resolve component "default" at ...— a hard crash.STALE_ASSET_ERROR_PATTERNS, so the PostHog suppression this code was written to provide never applied to the error it created. The realFailed to fetch dynamically imported modulecause was swallowed; only the downstream router error escaped.Changes
event.preventDefault()(and the now-pointlessstopImmediatePropagation()) from thevite:preloadErrorhandler so Vite rethrows. The rejection surfaces asFailed to fetch dynamically imported module, which the existing patterns insrc/services/staleAssetErrors.tsalready match — restoring both the auto-reload and the PostHog suppression that were intended.router.onErrorfallback that treatsCouldn't resolve componentas a chunk error too, so a navigation that races the asyncwindow.location.reload()still recovers instead of leaving a blank route.isComponentResolutionErrorMessagehelper + unit coverage.Note
Adding the
Couldn't resolve componentpattern to the PostHog suppression list on its own would only hide the symptom — the root-cause fix is removingpreventDefault(). Therouter.onErrorhandler catches the error before it propagates, so it no longer reaches PostHog's exception autocapture.Test plan
vitest run tests/stale-asset-errors.unit.test.ts— 6 passing (includes new component-resolution matcher cases).index.htmlis open → navigating to a lazy route now auto-reloads to the latest version instead of crashing onCouldn't resolve component.Screenshots
Checklist
Created with PostHog Desktop from this inbox report.
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.