What is best place to put externally hosted javascript for a specific page? #2001
-
I'm implementing Paddle's custom inline checkout, and as per the documentation (Import Paddje.js) the best place to put it is either in the head or body of your site. Is there a Remix specific way of adding externally hosted scripts that should only get loaded on a specific page? I've come up with the following solution that uses the // root.tsx
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
useMatches
} from "remix";
import type { MetaFunction } from "remix";
export const meta: MetaFunction = () => {
return { title: "New Remix App" };
};
export default function App() {
const matches = useMatches()
const isCheckingOut = matches.find(m => m.pathname.indexOf("/checkout") !== -1)
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
<Links />
</head>
<body>
<Outlet />
<ScrollRestoration />
<Scripts />
{isCheckingOut && <script src="https://cdn.paddle.com/paddle/paddle.js"></script>}
{process.env.NODE_ENV === "development" && <LiveReload />}
</body>
</html>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
That's a perfectly acceptable solution. If you want to make it more generic, take a look at It allows you to add the external scripts to the // routes/checkout.tsx
export const handle = {
scripts: () => [{ src: 'https://cdn.paddle.com/paddle/paddle.js' }]
} Then your root layout renders the |
Beta Was this translation helpful? Give feedback.
That's a perfectly acceptable solution. If you want to make it more generic, take a look at
remix-utils
<ExternalScripts/>
component.It allows you to add the external scripts to the
handle
export in your route.Then your root layout renders the
<ExternalScripts/>
component.https://github.com/sergiodxa/remix-utils#externalscripts