Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .github/workflows/pre-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
branches:
- main

permissions:
contents: read

jobs:
lint:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -33,6 +36,29 @@ jobs:
- name: Run plugins lint
run: yarn lint:plugins

check-vercel-redirects:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6

- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: "22"
cache: yarn

- name: Install dependencies
run: yarn install --immutable

- name: Check Vercel redirects are in sync
run: |
yarn sync-redirects:vercel
if ! git diff --exit-code vercel.json; then
echo "::error::vercel.json redirects are out of sync with _redirects. Run 'yarn sync-redirects:vercel' and commit."
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
exit 1
fi

lint-website:
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ website/build/
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.vercel
.env*.local
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
"lint:plugins": "yarn workspace @react-native-website/remark-codeblock-language-as-title lint && yarn workspace @react-native-website/remark-lint-no-broken-external-links lint && yarn workspace @react-native-website/remark-snackplayer lint && yarn workspace @react-native-website/remark-lint-no-broken-external-links test && yarn workspace @react-native-website/remark-snackplayer test",
"lint:website": "eslint ./website ./docs",
"update-lock": "npx yarn-deduplicate",
"check-dependencies": "manypkg check"
"check-dependencies": "manypkg check",
"sync-redirects:vercel": "node scripts/src/sync-vercel-redirects.ts",
"build:vercel": "yarn --cwd website build:vercel",
"build:vercel:fast": "yarn --cwd website build:vercel:fast",
"dev:vercel": "vercel dev"
},
"devDependencies": {
"@eslint/css": "^1.0.0",
Expand Down
124 changes: 124 additions & 0 deletions scripts/src/sync-vercel-redirects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import fs from 'node:fs';
import path from 'node:path';

const REPO_ROOT = path.resolve(import.meta.dirname, '../..');
const REDIRECTS_PATH = path.join(REPO_ROOT, 'website/static/_redirects');
const VERSIONS_PATH = path.join(REPO_ROOT, 'website/versions.json');
const VERCEL_JSON_PATH = path.join(REPO_ROOT, 'vercel.json');

interface VercelRedirect {
source: string;
destination: string;
permanent: boolean;
}

function isPartialSegmentWildcard(source: string): boolean {
return source.split('/').some(seg => seg.includes('*') && seg !== '*');
}

function expandPartialWildcard(
source: string,
destination: string
): VercelRedirect[] {
const segments = source.split('/');
const wildcardSeg = segments.find(seg => seg.includes('*') && seg !== '*')!;
const prefix = wildcardSeg.replaceAll('*', '');

// Infer docs directory from destination path
// e.g. /docs/next/legacy/native-modules-:splat → docs/legacy/
const destDir = destination
.replace(/^\/docs\/next\//, '')
.replace(/\/[^/]*$/, '');
const searchDir = path.join(REPO_ROOT, 'docs', destDir);

let files: string[];
try {
files = fs
.readdirSync(searchDir)
.filter(f => f.startsWith(prefix) && /\.mdx?$/.test(f))
.map(f => f.replace(/\.mdx?$/, ''));
} catch {
console.warn(
`Warning: Could not read ${searchDir} for expanding ${source}`
);
return [];
}

return files.map(filename => {
const suffix = filename.slice(prefix.length);
return {
source: source.replace(`${prefix}*`, filename),
destination: destination.replace(':splat', suffix),
permanent: true,
};
});
}

function syncRedirects(): void {
const latestVersion: string = JSON.parse(
fs.readFileSync(VERSIONS_PATH, 'utf8')
)[0];

const lines = fs.readFileSync(REDIRECTS_PATH, 'utf8').split('\n');
const redirects: VercelRedirect[] = [];

for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;

const parts = trimmed.split(/\s+/);
if (parts.length < 2) continue;

let [source, destination] = parts;

// Strip full URL sources to path only
if (source.startsWith('http://') || source.startsWith('https://')) {
try {
source = new URL(source).pathname;
} catch {
continue;
}
}

// Convert same-domain full URL destinations to relative paths
if (destination.startsWith('https://reactnative.dev/')) {
destination = destination.replace('https://reactnative.dev', '');
}

// Replace $LATEST_VERSION$ placeholder with actual version
source = source.replaceAll('$LATEST_VERSION$', latestVersion);
destination = destination.replaceAll('$LATEST_VERSION$', latestVersion);

// Handle partial-segment wildcards (e.g., native-modules-*)
// Vercel doesn't support wildcards within a path segment, so enumerate
if (isPartialSegmentWildcard(source)) {
redirects.push(...expandPartialWildcard(source, destination));
continue;
}

// Convert Netlify wildcard syntax to Vercel format
// Netlify: /* and :splat → Vercel: /:path*
source = source.replace(/\*$/, ':path*');
destination = destination.replace(':splat', ':path*');

redirects.push({source, destination, permanent: true});
}

const vercelJson = JSON.parse(fs.readFileSync(VERCEL_JSON_PATH, 'utf8'));
vercelJson.redirects = redirects;
fs.writeFileSync(
VERCEL_JSON_PATH,
JSON.stringify(vercelJson, null, 2) + '\n'
);

console.log(`Synced ${redirects.length} redirects to vercel.json`);
}

syncRedirects();
Loading
Loading