A React Router component for server-side script loading and rendering designed for React Router’s data mode and framework mode. This package provides secure, cached script loading with TypeScript support for server-side rendering.
React Router Modes
This package is designed specifically for:
- Data Mode: Using
createBrowserRouter with loaders
- Framework Mode: Using React Router with Vite plugin and SSR
This package is for server-side rendering only. The script loader will
throw an error if it runs in the browser, and it is not suitable for pure
client-side applications.
Installation
Every rendered <script> tag must include a data-code attribute set
to your Account ID (verification code). AdUnblock uses this value to verify
that the script is running on your registered domain. Find your Account ID
at the top of your AdUnblock dashboard.
Usage
Data Mode with Custom Server
Using server-tag-react-router in “Data Mode” requires a custom server-side rendering (SSR) setup. The serverTagLoader is designed to run exclusively on the server.
1. Define Your Routes (Shared)
This route configuration is used by both the server and the client.
2. Create a Server Entrypoint
This file contains the server-side rendering logic, typically run in an Express or Node.js environment.
3. Create a Client Entrypoint
This file hydrates the server-rendered HTML in the browser.
Framework Mode Usage (Optimal Solution)
For React Router applications using a framework with SSR (e.g., Vite’s SSR plugin), ServerTag offers a powerful and seamless way to manage both global and per-route scripts.
Simply place the <ServerTag /> component once in your root layout. It will automatically detect and render scripts loaded from any active route’s loader, from the root down to the deepest child.
Example: Hybrid Approach
This example demonstrates loading a global analytics script from the root and a page-specific charting library for a dashboard page.
1. Update Your Root Layout
Place <ServerTag /> in the <head> of your root layout. This single component will handle rendering all scripts.
2. Add a Page-Specific Loader
In a specific route, add another loader. When combining with other page data, ensure that the object returned by serverTagLoader is spread into the final returned object from your loader.
The remote URL should return a JSON response in this format:
Custom Script Rendering
Override the default script rendering with a custom callback:
API Reference
ServerTag Component
Loader Function
serverTagLoader(config)
Async function for loading scripts in React Router loaders. Works with both data mode and framework mode.
Parameters:
config.remoteUrl (string, required): The URL to fetch script URLs from
config.cacheInterval (number, optional): Cache duration in seconds (default: 300)
Returns: Promise<ServerTagLoaderData>
Usage Examples:
Types
Server-Side Only Design
This package is designed exclusively for server-side rendering:
- Loaders run on the server during route resolution
- Components render on the server before hydration
- Scripts are included in the initial HTML for optimal performance
- No client-side fetching or dynamic script injection
Security
- Only HTTP and HTTPS URLs are allowed
- URL validation prevents malicious protocol usage
- Server-side execution prevents client-side script injection
- Built-in error handling for failed requests
Browser Compatibility
This package works with all browsers supported by React 19 and React Router DOM 7+ when used in server-side rendering contexts.