Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Chrome tab restore (#4033)

Ivan Starkova25f2d7e9394
diff --git a/apps/builder/app/routes/_ui.(builder).tsx b/apps/builder/app/routes/_ui.(builder).tsx
index cd5e28fea3..0c3e30082e 100644
--- a/apps/builder/app/routes/_ui.(builder).tsx
+++ b/apps/builder/app/routes/_ui.(builder).tsx
@@ -6,7 +6,7 @@
import { lazy } from "react";
import { useLoaderData } from "@remix-run/react";
import type { MetaFunction, ShouldRevalidateFunction } from "@remix-run/react";
-import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
+import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { loadBuildIdAndVersionByProjectId } from "@webstudio-is/project-build/index.server";
import { db } from "@webstudio-is/project/index.server";
@@ -27,6 +27,7 @@ import { ClientOnly } from "~/shared/client-only";
import type { BuilderProps } from "~/builder/builder";
import { parseBuilderUrl } from "~/shared/router-utils/origins";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect } from "~/services/no-store-redirect";
export const links = () => {
return [
diff --git a/apps/builder/app/routes/_ui.dashboard.tsx b/apps/builder/app/routes/_ui.dashboard.tsx
index cc30085c4a..0f05b423ce 100644
--- a/apps/builder/app/routes/_ui.dashboard.tsx
+++ b/apps/builder/app/routes/_ui.dashboard.tsx
@@ -1,10 +1,6 @@
import { lazy } from "react";
import { useLoaderData, type MetaFunction } from "@remix-run/react";
-import {
- json,
- type LoaderFunctionArgs,
- redirect,
-} from "@remix-run/server-runtime";
+import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { dashboardProjectRouter } from "@webstudio-is/dashboard/index.server";
import { findAuthenticatedUser } from "~/services/auth.server";
import { isDashboard, loginPath } from "~/shared/router-utils";
@@ -13,6 +9,7 @@ import env from "~/env/env.server";
import { ClientOnly } from "~/shared/client-only";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
import { createCallerFactory } from "@webstudio-is/trpc-interface/index.server";
+import { redirect } from "~/services/no-store-redirect";
const dashboardProjectCaller = createCallerFactory(dashboardProjectRouter);
diff --git a/apps/builder/app/routes/_ui.error.tsx b/apps/builder/app/routes/_ui.error.tsx
index 5b70f40021..cb6ce5ebd5 100644
--- a/apps/builder/app/routes/_ui.error.tsx
+++ b/apps/builder/app/routes/_ui.error.tsx
@@ -43,9 +43,13 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
? parsedError.data
: {
message: "Unknown error",
- description: fromError(parsedError.error).toString(),
+ description: "",
};
+ if (false === parsedError.success) {
+ console.error(fromError(parsedError.error));
+ }
+
return json(
{ error, origin: getAuthorizationServerOrigin(request.url) },
// remove flash message from session
diff --git a/apps/builder/app/routes/_ui.login._index.tsx b/apps/builder/app/routes/_ui.login._index.tsx
index 6d4de14c88..4eee7b44b0 100644
--- a/apps/builder/app/routes/_ui.login._index.tsx
+++ b/apps/builder/app/routes/_ui.login._index.tsx
@@ -1,7 +1,6 @@
import {
type LoaderFunctionArgs,
type TypedResponse,
- redirect,
json,
} from "@remix-run/server-runtime";
import { useLoaderData, type MetaFunction } from "@remix-run/react";
@@ -18,6 +17,7 @@ import { returnToCookie } from "~/services/cookie.server";
import { ClientOnly } from "~/shared/client-only";
import { lazy } from "react";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect } from "~/services/no-store-redirect";
export const meta: MetaFunction<typeof loader> = () => {
const metas: ReturnType<MetaFunction> = [];
diff --git a/apps/builder/app/routes/auth.dev.tsx b/apps/builder/app/routes/auth.dev.tsx
index a85d0c5fdf..365c8b1482 100644
--- a/apps/builder/app/routes/auth.dev.tsx
+++ b/apps/builder/app/routes/auth.dev.tsx
@@ -1,9 +1,10 @@
-import { type ActionFunctionArgs, redirect } from "@remix-run/server-runtime";
+import { type ActionFunctionArgs } from "@remix-run/server-runtime";
import { authenticator } from "~/services/auth.server";
import { dashboardPath, isDashboard, loginPath } from "~/shared/router-utils";
import { AUTH_PROVIDERS } from "~/shared/session";
import { clearReturnToCookie, returnToPath } from "~/services/cookie.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect, setNoStoreToRedirect } from "~/services/no-store-redirect";
export default function Dev() {
return null;
@@ -29,7 +30,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
} catch (error: unknown) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- return clearReturnToCookie(request, error);
+ return setNoStoreToRedirect(await clearReturnToCookie(request, error));
}
if (error instanceof Error) {
diff --git a/apps/builder/app/routes/auth.github.tsx b/apps/builder/app/routes/auth.github.tsx
index af5e8d083b..52573df910 100644
--- a/apps/builder/app/routes/auth.github.tsx
+++ b/apps/builder/app/routes/auth.github.tsx
@@ -1,9 +1,10 @@
-import { type ActionFunctionArgs, redirect } from "@remix-run/server-runtime";
+import { type ActionFunctionArgs } from "@remix-run/server-runtime";
import { authenticator } from "~/services/auth.server";
import { dashboardPath, isDashboard, loginPath } from "~/shared/router-utils";
import { AUTH_PROVIDERS } from "~/shared/session";
import { clearReturnToCookie, returnToPath } from "~/services/cookie.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect, setNoStoreToRedirect } from "~/services/no-store-redirect";
export default function GH() {
return null;
@@ -29,7 +30,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
} catch (error) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- return clearReturnToCookie(request, error);
+ return setNoStoreToRedirect(await clearReturnToCookie(request, error));
}
const message = error instanceof Error ? error?.message : "unknown error";
diff --git a/apps/builder/app/routes/auth.github_.callback.tsx b/apps/builder/app/routes/auth.github_.callback.tsx
index e7ad63c2dc..a4fffbf868 100644
--- a/apps/builder/app/routes/auth.github_.callback.tsx
+++ b/apps/builder/app/routes/auth.github_.callback.tsx
@@ -1,9 +1,10 @@
-import { type LoaderFunctionArgs, redirect } from "@remix-run/server-runtime";
+import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { authenticator } from "~/services/auth.server";
import { dashboardPath, isDashboard, loginPath } from "~/shared/router-utils";
import { AUTH_PROVIDERS } from "~/shared/session";
import { clearReturnToCookie, returnToPath } from "~/services/cookie.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect, setNoStoreToRedirect } from "~/services/no-store-redirect";
export const loader = async ({ request }: LoaderFunctionArgs) => {
preventCrossOriginCookie(request);
@@ -25,7 +26,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
} catch (error) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- return clearReturnToCookie(request, error);
+ return setNoStoreToRedirect(await clearReturnToCookie(request, error));
}
const message = error instanceof Error ? error?.message : "unknown error";
diff --git a/apps/builder/app/routes/auth.google.tsx b/apps/builder/app/routes/auth.google.tsx
index 123c7717db..76906a315e 100644
--- a/apps/builder/app/routes/auth.google.tsx
+++ b/apps/builder/app/routes/auth.google.tsx
@@ -1,13 +1,13 @@
import {
type ActionFunctionArgs,
type LoaderFunctionArgs,
- redirect,
} from "@remix-run/server-runtime";
import { authenticator } from "~/services/auth.server";
import { dashboardPath, isDashboard, loginPath } from "~/shared/router-utils";
import { AUTH_PROVIDERS } from "~/shared/session";
import { clearReturnToCookie, returnToPath } from "~/services/cookie.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect, setNoStoreToRedirect } from "~/services/no-store-redirect";
export default function Google() {
return null;
@@ -35,7 +35,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {
} catch (error) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- return clearReturnToCookie(request, error);
+ return setNoStoreToRedirect(await clearReturnToCookie(request, error));
}
const message = error instanceof Error ? error?.message : "unknown error";
diff --git a/apps/builder/app/routes/auth.google_.callback.tsx b/apps/builder/app/routes/auth.google_.callback.tsx
index 463de7a148..67878c8e8b 100644
--- a/apps/builder/app/routes/auth.google_.callback.tsx
+++ b/apps/builder/app/routes/auth.google_.callback.tsx
@@ -1,9 +1,10 @@
-import { redirect, type LoaderFunctionArgs } from "@remix-run/server-runtime";
+import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { authenticator } from "~/services/auth.server";
import { dashboardPath, isDashboard, loginPath } from "~/shared/router-utils";
import { clearReturnToCookie, returnToPath } from "~/services/cookie.server";
import { AUTH_PROVIDERS } from "~/shared/session";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect, setNoStoreToRedirect } from "~/services/no-store-redirect";
export const loader = async ({ request }: LoaderFunctionArgs) => {
preventCrossOriginCookie(request);
@@ -25,7 +26,7 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {
} catch (error) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- return clearReturnToCookie(request, error);
+ return setNoStoreToRedirect(await clearReturnToCookie(request, error));
}
const message = error instanceof Error ? error?.message : "unknown error";
diff --git a/apps/builder/app/routes/auth.ws.ts b/apps/builder/app/routes/auth.ws.ts
index f00884e1bc..e0737da439 100644
--- a/apps/builder/app/routes/auth.ws.ts
+++ b/apps/builder/app/routes/auth.ws.ts
@@ -4,6 +4,7 @@ import { builderAuthenticator } from "~/services/builder-auth.server";
import { comparePathnames, isBuilder } from "~/shared/router-utils";
import { isRedirectResponse, returnToCookie } from "~/services/cookie.server";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { setNoStoreToRedirect } from "~/services/no-store-redirect";
const debug = createDebug(import.meta.url);
@@ -31,9 +32,9 @@ export const loader: LoaderFunction = async ({ request }) => {
} catch (error) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- const response = error.clone();
+ if (isRedirectResponse(error)) {
+ const response = setNoStoreToRedirect(error).clone();
- if (isRedirectResponse(response)) {
const url = new URL(request.url);
let returnTo = url.searchParams.get("returnTo");
if (returnTo !== null && comparePathnames(returnTo, request.url)) {
@@ -47,9 +48,11 @@ export const loader: LoaderFunction = async ({ request }) => {
"Set-Cookie",
await returnToCookie.serialize(returnTo, options)
);
+
+ return response;
}
- return response;
+ return error;
}
debug("error", error);
diff --git a/apps/builder/app/routes/auth.ws_.callback.ts b/apps/builder/app/routes/auth.ws_.callback.ts
index 6d41bf8f58..92fa88f65f 100644
--- a/apps/builder/app/routes/auth.ws_.callback.ts
+++ b/apps/builder/app/routes/auth.ws_.callback.ts
@@ -1,4 +1,4 @@
-import { type LoaderFunction, redirect } from "@remix-run/server-runtime";
+import { type LoaderFunction } from "@remix-run/server-runtime";
import { AuthorizationError } from "remix-auth";
import { createDebug } from "~/shared/debug";
import { clearReturnToCookie, returnToPath } from "~/services/cookie.server";
@@ -7,6 +7,7 @@ import { OAuth2Error } from "remix-auth-oauth2";
import { builderSessionStorage } from "~/services/builder-session.server";
import { builderPath, isBuilder } from "~/shared/router-utils";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect, setNoStoreToRedirect } from "~/services/no-store-redirect";
const debug = createDebug(import.meta.url);
@@ -34,7 +35,7 @@ export const loader: LoaderFunction = async ({ request }) => {
} catch (error) {
// all redirects are basically errors and in that case we don't want to catch it
if (error instanceof Response) {
- debug("Response Cookie", error.headers.get("Set-Cookie"));
+ debug("Response with Cookie");
const session = await builderSessionStorage.getSession(
error.headers.get("Set-Cookie")
@@ -52,7 +53,7 @@ export const loader: LoaderFunction = async ({ request }) => {
await builderSessionStorage.commitSession(session)
);
- return clearReturnToCookie(request, response);
+ return setNoStoreToRedirect(await clearReturnToCookie(request, response));
}
if (error instanceof AuthorizationError) {
diff --git a/apps/builder/app/routes/builder.$projectId.tsx b/apps/builder/app/routes/builder.$projectId.tsx
index 66731e69b1..0af4a78343 100644
--- a/apps/builder/app/routes/builder.$projectId.tsx
+++ b/apps/builder/app/routes/builder.$projectId.tsx
@@ -1,4 +1,5 @@
-import { redirect, type LoaderFunctionArgs } from "@remix-run/server-runtime";
+import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
+import { redirect } from "~/services/no-store-redirect";
import { builderUrl } from "~/shared/router-utils";
// Support for Auth Token URLs generated by previous Builder versions
diff --git a/apps/builder/app/routes/n8n.$.tsx b/apps/builder/app/routes/n8n.$.tsx
index 0b2d5714ef..95923a4fbb 100644
--- a/apps/builder/app/routes/n8n.$.tsx
+++ b/apps/builder/app/routes/n8n.$.tsx
@@ -1,8 +1,4 @@
-import {
- type LoaderFunctionArgs,
- json,
- redirect,
-} from "@remix-run/server-runtime";
+import { type LoaderFunctionArgs, json } from "@remix-run/server-runtime";
import { isRouteErrorResponse, useRouteError } from "@remix-run/react";
import { z } from "zod";
import { findAuthenticatedUser } from "~/services/auth.server";
@@ -10,6 +6,7 @@ import { isDashboard, loginPath } from "~/shared/router-utils";
import env from "~/env/env.server";
import cookie from "cookie";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect } from "~/services/no-store-redirect";
const zN8NResponse = z.union([
z.object({
diff --git a/apps/builder/app/routes/oauth.ws.authorize.tsx b/apps/builder/app/routes/oauth.ws.authorize.tsx
index d61afa7cf9..bfe6233321 100644
--- a/apps/builder/app/routes/oauth.ws.authorize.tsx
+++ b/apps/builder/app/routes/oauth.ws.authorize.tsx
@@ -1,4 +1,4 @@
-import { json, redirect, type LoaderFunction } from "@remix-run/server-runtime";
+import { json, type LoaderFunction } from "@remix-run/server-runtime";
import { z } from "zod";
import { createDebug } from "~/shared/debug";
import { fromError } from "zod-validation-error";
@@ -10,6 +10,7 @@ import { isUserAuthorizedForProject } from "~/services/builder-access.server";
import { loginPath } from "~/shared/router-utils";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
import * as session from "~/services/session.server";
+import { redirect } from "~/services/no-store-redirect";
const debug = createDebug(import.meta.url);
@@ -194,12 +195,7 @@ export const loader: LoaderFunction = async ({ request }) => {
return session.writeLoginSessionBloomFilter(
request,
- new Response(null, {
- status: 302,
- headers: {
- Location: redirectUri.href,
- },
- }),
+ redirect(redirectUri.href),
bloomFilter
);
}
diff --git a/apps/builder/app/routes/rest.current.products.ts b/apps/builder/app/routes/rest.current.products.ts
index 95c1706848..129c3f58e0 100644
--- a/apps/builder/app/routes/rest.current.products.ts
+++ b/apps/builder/app/routes/rest.current.products.ts
@@ -1,12 +1,9 @@
-import {
- json,
- redirect,
- type LoaderFunctionArgs,
-} from "@remix-run/server-runtime";
+import { json, type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { findAuthenticatedUser } from "~/services/auth.server";
import { loginPath } from "~/shared/router-utils";
import { prisma } from "@webstudio-is/prisma-client";
import { preventCrossOriginCookie } from "~/services/no-cross-origin-cookie";
+import { redirect } from "~/services/no-store-redirect";
/**
* Created for ebugging purposes, to check that payments and products are working
diff --git a/apps/builder/app/services/no-store-redirect.ts b/apps/builder/app/services/no-store-redirect.ts
new file mode 100644
index 0000000000..053f38725f
--- /dev/null
+++ b/apps/builder/app/services/no-store-redirect.ts
@@ -0,0 +1,43 @@
+import { redirect as remixRedirect } from "@remix-run/server-runtime";
+import { isRedirectResponse } from "./cookie.server";
+
+/**
+ * Chrome aggressively uses cache when restoring tabs (e.g., using Shift+Command+T or automatic session restore).
+ * This behavior can cause issues with loading a builder application due to the following cycle:
+ *
+ * 1. The builder checks if the session needs to be renewed (a navigation is detected) and redirects to `/auth/ws`.
+ * 2. Chrome retrieves the `/auth/ws` response from the cache, followed by all OAuth redirect responses from the cache.
+ * 3. The last cached response redirects back to the builder's root (`/`). Chrome detects this as a redirect cycle
+ * and reloads the most recent OAuth step, resulting in an "expected state" error.
+ * 4. To prevent this issue, the only solution is to add a `no-store` directive to all redirect responses.
+ */
+export const redirect: typeof remixRedirect = (url, init) => {
+ const headers =
+ typeof init === "object" ? new Headers(init.headers) : new Headers();
+ headers.set("Cache-Control", "no-store");
+
+ const responseInit: ResponseInit =
+ typeof init === "number" ? { status: init, headers } : { ...init, headers };
+
+ return remixRedirect(url, responseInit);
+};
+
+/**
+ * Chrome aggressively uses cache when restoring tabs (e.g., using Shift+Command+T or automatic session restore).
+ * This behavior can cause issues with loading a builder application due to the following cycle:
+ *
+ * 1. The builder checks if the session needs to be renewed (a navigation is detected) and redirects to `/auth/ws`.
+ * 2. Chrome retrieves the `/auth/ws` response from the cache, followed by all OAuth redirect responses from the cache.
+ * 3. The last cached response redirects back to the builder's root (`/`). Chrome detects this as a redirect cycle
+ * and reloads the most recent OAuth step, resulting in an "expected state" error.
+ * 4. To prevent this issue, the only solution is to add a `no-store` directive to all redirect responses.
+ */
+export const setNoStoreToRedirect = (response: Response) => {
+ if (isRedirectResponse(response)) {
+ const newResponse = response.clone();
+ newResponse.headers.set("Cache-Control", "no-store");
+ return newResponse;
+ }
+
+ return response;
+};