Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.3 KB
Raw Blame
import { unmappedStableChunkSpecifier } from './isChunkLoadError'

/** Only the stable chunk build renders an import map (see posthog/stable_chunks.py). */
export function isStableChunkBuild(): boolean {
    return document.querySelector('script[type="importmap"]') !== null
}

/**
 * Reloads the page after a chunk failed to load. On the stable build, a plain reload gets the same
 * import map and fails again while a new stable chunk is not reachable, so the reload asks for the
 * default build. The server always serves the default build for ?stable_chunks=fallback and stores
 * no choice for it, so this cannot loop, and the next page load tries the stable build again.
 * The reload names a specifier that the import map did not hold, so the server can tell a page
 * whose import map misses a chunk apart from a chunk that failed to load.
 */
export function reloadAfterChunkLoadError(error: unknown): void {
    if (!isStableChunkBuild()) {
        window.location.reload()
        return
    }
    const url = new URL(window.location.href)
    url.searchParams.set('stable_chunks', 'fallback')
    const unmapped = unmappedStableChunkSpecifier(error)
    if (unmapped) {
        url.searchParams.set('stable_chunks_unmapped', unmapped)
    }
    window.location.replace(url.toString())
}