mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 19:33:11 +02:00
Lazy-load Stripe SDK so it only loads on checkout (#6546)
# Description of Changes - Stripe SDK (`@stripe/*` + `js.stripe.com/v3`) was loading on every page; now it only loads when an upgrade/checkout modal actually opens. - Converted every import site to `React.lazy()` + `<Suspense>`, gated by the existing `opened` state. - Adds a Playwright spec that asserts no Stripe requests on landing or settings. --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have run `task check` to verify linters, typechecks, and tests pass - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#7-testing) for more details.
This commit is contained in:
@@ -0,0 +1,81 @@
|
|||||||
|
import { test, expect } from "@app/tests/helpers/stub-test-base";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies that the Stripe SDK (@stripe/stripe-js, @stripe/react-stripe-js,
|
||||||
|
* and the js.stripe.com remote script) is NOT fetched on cold page loads —
|
||||||
|
* only when the checkout modal actually mounts. The proprietary
|
||||||
|
* CheckoutProvider, the SaaS TrialStatusBanner, and the SaaS Plan settings
|
||||||
|
* page all gate the modal behind React.lazy + a conditional render so the
|
||||||
|
* Stripe chunk lives in its own async bundle.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const STRIPE_URL_FRAGMENTS = [
|
||||||
|
"js.stripe.com",
|
||||||
|
"@stripe/stripe-js",
|
||||||
|
// Vite's optimizeDeps id mangles slashes to underscores when serving
|
||||||
|
// pre-bundled dependencies from node_modules/.vite/deps.
|
||||||
|
"@stripe_stripe-js",
|
||||||
|
"@stripe/react-stripe-js",
|
||||||
|
"@stripe_react-stripe-js",
|
||||||
|
];
|
||||||
|
|
||||||
|
function isStripeRequest(url: string): boolean {
|
||||||
|
return STRIPE_URL_FRAGMENTS.some((fragment) => url.includes(fragment));
|
||||||
|
}
|
||||||
|
|
||||||
|
test.describe("Stripe SDK lazy loading", () => {
|
||||||
|
test("landing page does not fetch Stripe SDK on cold load", async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
const stripeRequests: string[] = [];
|
||||||
|
page.on("request", (req) => {
|
||||||
|
const url = req.url();
|
||||||
|
if (isStripeRequest(url)) {
|
||||||
|
stripeRequests.push(url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// The stub fixture already navigated to "/" with waitUntil:
|
||||||
|
// domcontentloaded. Wait for the app to actually settle so any lurking
|
||||||
|
// module-eval-time loadStripe() call would have already kicked off.
|
||||||
|
await page
|
||||||
|
.waitForLoadState("networkidle", { timeout: 15_000 })
|
||||||
|
.catch(() => {
|
||||||
|
// Posthog / iconify keep some connections warm — fall back to a
|
||||||
|
// brief settle window if networkidle never resolves.
|
||||||
|
});
|
||||||
|
await page.waitForTimeout(2_000);
|
||||||
|
|
||||||
|
expect(
|
||||||
|
stripeRequests,
|
||||||
|
`Expected no Stripe-related network activity on landing, but observed:\n${stripeRequests.join("\n")}`,
|
||||||
|
).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("settings modal does not fetch Stripe SDK", async ({ page }) => {
|
||||||
|
const stripeRequests: string[] = [];
|
||||||
|
page.on("request", (req) => {
|
||||||
|
const url = req.url();
|
||||||
|
if (isStripeRequest(url)) {
|
||||||
|
stripeRequests.push(url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Opening Settings is the closest a default proprietary user gets to
|
||||||
|
// checkout without actually clicking Upgrade. Even rendering the
|
||||||
|
// settings drawer must NOT pull Stripe into the entry path — only the
|
||||||
|
// upgrade modal itself, which sits one click further in.
|
||||||
|
const settingsButton = page
|
||||||
|
.getByRole("button", { name: /settings/i })
|
||||||
|
.first();
|
||||||
|
if (await settingsButton.isVisible({ timeout: 5_000 }).catch(() => false)) {
|
||||||
|
await settingsButton.click();
|
||||||
|
await page.waitForTimeout(1_500);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(
|
||||||
|
stripeRequests,
|
||||||
|
`Expected no Stripe-related network activity after opening settings, but observed:\n${stripeRequests.join("\n")}`,
|
||||||
|
).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -4,6 +4,8 @@ import React, {
|
|||||||
useState,
|
useState,
|
||||||
useCallback,
|
useCallback,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
lazy,
|
||||||
|
Suspense,
|
||||||
ReactNode,
|
ReactNode,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
@@ -13,7 +15,12 @@ import licenseService, {
|
|||||||
mapLicenseToTier,
|
mapLicenseToTier,
|
||||||
PlanTier,
|
PlanTier,
|
||||||
} from "@app/services/licenseService";
|
} from "@app/services/licenseService";
|
||||||
import { StripeCheckout } from "@app/components/shared/stripeCheckout";
|
|
||||||
|
const StripeCheckout = lazy(() =>
|
||||||
|
import("@app/components/shared/stripeCheckout").then((m) => ({
|
||||||
|
default: m.StripeCheckout,
|
||||||
|
})),
|
||||||
|
);
|
||||||
import { userManagementService } from "@app/services/userManagementService";
|
import { userManagementService } from "@app/services/userManagementService";
|
||||||
import { alert } from "@app/components/toast";
|
import { alert } from "@app/components/toast";
|
||||||
import {
|
import {
|
||||||
@@ -433,6 +440,7 @@ export const CheckoutProvider: React.FC<CheckoutProviderProps> = ({
|
|||||||
|
|
||||||
{/* Global Checkout Modal */}
|
{/* Global Checkout Modal */}
|
||||||
{selectedPlanGroup && (
|
{selectedPlanGroup && (
|
||||||
|
<Suspense fallback={null}>
|
||||||
<StripeCheckout
|
<StripeCheckout
|
||||||
opened={isOpen}
|
opened={isOpen}
|
||||||
onClose={closeCheckout}
|
onClose={closeCheckout}
|
||||||
@@ -443,6 +451,7 @@ export const CheckoutProvider: React.FC<CheckoutProviderProps> = ({
|
|||||||
onLicenseActivated={handleLicenseActivated}
|
onLicenseActivated={handleLicenseActivated}
|
||||||
hostedCheckoutSuccess={hostedCheckoutSuccess}
|
hostedCheckoutSuccess={hostedCheckoutSuccess}
|
||||||
/>
|
/>
|
||||||
|
</Suspense>
|
||||||
)}
|
)}
|
||||||
</CheckoutContext.Provider>
|
</CheckoutContext.Provider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { lazy, Suspense, useEffect, useState } from "react";
|
||||||
import { useAuth } from "@app/auth/UseSession";
|
import { useAuth } from "@app/auth/UseSession";
|
||||||
import { TrialExpiredModal } from "@app/components/shared/TrialExpiredModal";
|
import { TrialExpiredModal } from "@app/components/shared/TrialExpiredModal";
|
||||||
import StripeCheckout from "@app/components/shared/StripeCheckoutSaas";
|
|
||||||
|
const StripeCheckout = lazy(
|
||||||
|
() => import("@app/components/shared/StripeCheckoutSaas"),
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bootstrap component that shows the trial expired modal when a user's trial has ended
|
* Bootstrap component that shows the trial expired modal when a user's trial has ended
|
||||||
@@ -106,7 +109,8 @@ export default function TrialExpiredBootstrap() {
|
|||||||
onSubscribe={handleSubscribe}
|
onSubscribe={handleSubscribe}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{user && (
|
{user && checkoutOpened && (
|
||||||
|
<Suspense fallback={null}>
|
||||||
<StripeCheckout
|
<StripeCheckout
|
||||||
opened={checkoutOpened}
|
opened={checkoutOpened}
|
||||||
onClose={handleCheckoutClose}
|
onClose={handleCheckoutClose}
|
||||||
@@ -120,6 +124,7 @@ export default function TrialExpiredBootstrap() {
|
|||||||
}
|
}
|
||||||
isTrialConversion={false} // Trial already ended, so this is not a conversion
|
isTrialConversion={false} // Trial already ended, so this is not a conversion
|
||||||
/>
|
/>
|
||||||
|
</Suspense>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
import { useEffect, useState, useCallback } from "react";
|
import { lazy, Suspense, useEffect, useState, useCallback } from "react";
|
||||||
import { useBanner } from "@app/contexts/BannerContext";
|
import { useBanner } from "@app/contexts/BannerContext";
|
||||||
import { useAuth } from "@app/auth/UseSession";
|
import { useAuth } from "@app/auth/UseSession";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { InfoBanner } from "@app/components/shared/InfoBanner";
|
import { InfoBanner } from "@app/components/shared/InfoBanner";
|
||||||
import StripeCheckout from "@app/components/shared/StripeCheckoutSaas";
|
|
||||||
import { BASE_PATH } from "@app/constants/app";
|
import { BASE_PATH } from "@app/constants/app";
|
||||||
|
|
||||||
|
const StripeCheckout = lazy(
|
||||||
|
() => import("@app/components/shared/StripeCheckoutSaas"),
|
||||||
|
);
|
||||||
|
|
||||||
const SESSION_STORAGE_KEY = "trialBannerDismissed";
|
const SESSION_STORAGE_KEY = "trialBannerDismissed";
|
||||||
|
|
||||||
export function TrialStatusBanner() {
|
export function TrialStatusBanner() {
|
||||||
@@ -113,7 +116,8 @@ export function TrialStatusBanner() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{trialStatus && (
|
{trialStatus && checkoutOpen && (
|
||||||
|
<Suspense fallback={null}>
|
||||||
<StripeCheckout
|
<StripeCheckout
|
||||||
opened={checkoutOpen}
|
opened={checkoutOpen}
|
||||||
onClose={() => setCheckoutOpen(false)}
|
onClose={() => setCheckoutOpen(false)}
|
||||||
@@ -125,6 +129,7 @@ export function TrialStatusBanner() {
|
|||||||
onError={(error) => console.error("Checkout error:", error)}
|
onError={(error) => console.error("Checkout error:", error)}
|
||||||
isTrialConversion={true}
|
isTrialConversion={true}
|
||||||
/>
|
/>
|
||||||
|
</Suspense>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
import React, { useState, useCallback, useEffect } from "react";
|
import React, { lazy, Suspense, useState, useCallback, useEffect } from "react";
|
||||||
import { Divider, Loader, Alert, Select, Group, Text } from "@mantine/core";
|
import { Divider, Loader, Alert, Select, Group, Text } from "@mantine/core";
|
||||||
import { usePlans, PlanTier } from "@app/hooks/usePlans";
|
import { usePlans, PlanTier } from "@app/hooks/usePlans";
|
||||||
import StripeCheckout, {
|
import type {
|
||||||
PurchaseType,
|
PurchaseType,
|
||||||
CreditsPack,
|
CreditsPack,
|
||||||
PlanID,
|
PlanID,
|
||||||
} from "@app/components/shared/StripeCheckoutSaas";
|
} from "@app/components/shared/StripeCheckoutSaas";
|
||||||
|
|
||||||
|
const StripeCheckout = lazy(
|
||||||
|
() => import("@app/components/shared/StripeCheckoutSaas"),
|
||||||
|
);
|
||||||
import AvailablePlansSection from "@app/components/shared/config/configSections/plan/AvailablePlansSection";
|
import AvailablePlansSection from "@app/components/shared/config/configSections/plan/AvailablePlansSection";
|
||||||
import ApiPackagesSection from "@app/components/shared/config/configSections/plan/ApiPackagesSection";
|
import ApiPackagesSection from "@app/components/shared/config/configSections/plan/ApiPackagesSection";
|
||||||
import ActivePlanSection from "@app/components/shared/config/configSections/plan/ActivePlanSection";
|
import ActivePlanSection from "@app/components/shared/config/configSections/plan/ActivePlanSection";
|
||||||
@@ -204,22 +208,27 @@ const Plan: React.FC = () => {
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Stripe Checkout Modal */}
|
{/* Stripe Checkout Modal */}
|
||||||
|
{checkoutOpen &&
|
||||||
|
(selectedPlan !== null || selectedCreditsPack !== null) && (
|
||||||
|
<Suspense fallback={null}>
|
||||||
<StripeCheckout
|
<StripeCheckout
|
||||||
opened={
|
opened={true}
|
||||||
checkoutOpen &&
|
|
||||||
(selectedPlan !== null || selectedCreditsPack !== null)
|
|
||||||
}
|
|
||||||
onClose={handleCheckoutClose}
|
onClose={handleCheckoutClose}
|
||||||
purchaseType={purchaseType}
|
purchaseType={purchaseType}
|
||||||
planId={
|
planId={
|
||||||
purchaseType === "subscription" ? (selectedPlan?.id as PlanID) : null
|
purchaseType === "subscription"
|
||||||
|
? (selectedPlan?.id as PlanID)
|
||||||
|
: null
|
||||||
|
}
|
||||||
|
creditsPack={
|
||||||
|
purchaseType === "credits" ? selectedCreditsPack : null
|
||||||
}
|
}
|
||||||
creditsPack={purchaseType === "credits" ? selectedCreditsPack : null}
|
|
||||||
planName={
|
planName={
|
||||||
purchaseType === "subscription"
|
purchaseType === "subscription"
|
||||||
? selectedPlan?.name || ""
|
? selectedPlan?.name || ""
|
||||||
: data?.apiPackages.find((pkg) => pkg.id === selectedCreditsPack)
|
: data?.apiPackages.find(
|
||||||
?.name || ""
|
(pkg) => pkg.id === selectedCreditsPack,
|
||||||
|
)?.name || ""
|
||||||
}
|
}
|
||||||
onSuccess={handlePaymentSuccess}
|
onSuccess={handlePaymentSuccess}
|
||||||
onError={handlePaymentError}
|
onError={handlePaymentError}
|
||||||
@@ -227,6 +236,8 @@ const Plan: React.FC = () => {
|
|||||||
trialStatus?.isTrialing && purchaseType === "subscription"
|
trialStatus?.isTrialing && purchaseType === "subscription"
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
</Suspense>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { Button, Card, Text, Stack, Flex, Slider } from "@mantine/core";
|
import { Button, Card, Text, Stack, Flex, Slider } from "@mantine/core";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { CreditsPack } from "@app/components/shared/StripeCheckoutSaas";
|
import type { CreditsPack } from "@app/components/shared/StripeCheckoutSaas";
|
||||||
|
|
||||||
interface ApiPackage {
|
interface ApiPackage {
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user