Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(ci): stop shipping template-assets into every PR preview (#5565)

Cindy Zhang4e1b670b9067
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 55ff7449ca..19734dcbe3 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -351,7 +351,7 @@ jobs:
- name: Build core package
run: pnpm build
- - name: Compute sandbox preview path
+ - name: Compute sandbox preview paths
id: urls
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
@@ -359,8 +359,11 @@ jobs:
SHORT_HASH="${COMMIT_HASH:0:7}"
REPO_NAME="${{ github.event.repository.name }}"
- echo "short_hash=${SHORT_HASH}" >> $GITHUB_OUTPUT
- echo "sandbox_base_path=/${REPO_NAME}/pr/${PR_NUMBER}/sandbox" >> $GITHUB_OUTPUT
+ {
+ echo "short_hash=${SHORT_HASH}"
+ echo "sandbox_base_path=/${REPO_NAME}/pr/${PR_NUMBER}/sandbox"
+ echo "template_assets_base_path=/${REPO_NAME}/sandbox/template-assets"
+ } >> "$GITHUB_OUTPUT"
- name: Cache Next.js build
uses: actions/cache@v6
@@ -391,8 +394,12 @@ jobs:
# Built without the /<repo> prefix, the sandbox's root-absolute asset URLs
# drop that segment and 404 — breaking all styles/JS on the preview.
SANDBOX_BASE_PATH: ${{ steps.urls.outputs.sandbox_base_path }}
+ SANDBOX_TEMPLATE_ASSETS_BASE_PATH: ${{ steps.urls.outputs.template_assets_base_path }}
NODE_OPTIONS: '--max-old-space-size=8192'
+ - name: Remove shared assets from preview artifact
+ run: rm -rf apps/sandbox/out/template-assets
+
- name: Upload Sandbox artifact
uses: actions/upload-artifact@v7
with:
diff --git a/.github/workflows/cleanup-previews.yml b/.github/workflows/cleanup-previews.yml
index e682e5e89d..6047f2fab6 100644
--- a/.github/workflows/cleanup-previews.yml
+++ b/.github/workflows/cleanup-previews.yml
@@ -153,6 +153,27 @@ jobs:
echo ""
fi
+ # ----- Drop template-assets from previews -----
+ # Every preview uses the one shared /sandbox/template-assets copy.
+ # Sweep duplicate copies left by earlier deploys.
+ if [ -d "pr" ]; then
+ TA_PURGED=0
+ for ta in pr/*/sandbox/template-assets; do
+ [ -d "$ta" ] || continue
+ echo " DELETE ${ta} (duplicate preview copy)"
+ if [ "$DRY_RUN" != "true" ]; then
+ git rm -rf --quiet "$ta"
+ fi
+ DELETED_PATHS+=("$ta")
+ TA_PURGED=$((TA_PURGED + 1))
+ DELETED=$((DELETED + 1))
+ done
+ if [ "$TA_PURGED" -gt 0 ]; then
+ echo "Purged template-assets from ${TA_PURGED} preview(s)"
+ echo ""
+ fi
+ fi
+
# ----- Clean stale root-level files -----
# Leftovers from old Storybook-at-root deploy format
ROOT_STALE=0
diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml
index a0daeefc48..fc2b170aea 100644
--- a/.github/workflows/deploy-preview.yml
+++ b/.github/workflows/deploy-preview.yml
@@ -148,7 +148,9 @@ jobs:
mkdir -p /tmp/gh-pages/pr/${PR_NUMBER}
cp -r storybook-dist/* /tmp/gh-pages/pr/${PR_NUMBER}/
mkdir -p /tmp/gh-pages/pr/${PR_NUMBER}/sandbox
- cp -r sandbox-dist/* /tmp/gh-pages/pr/${PR_NUMBER}/sandbox/
+ # Every preview points at the one shared main-sandbox asset copy.
+ find sandbox-dist -mindepth 1 -maxdepth 1 ! -name template-assets \
+ -exec cp -r {} /tmp/gh-pages/pr/${PR_NUMBER}/sandbox/ \;
cd /tmp/gh-pages
git add -A
diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
index 8f3ff9b049..4d924800c1 100644
--- a/.github/workflows/deploy.yml
+++ b/.github/workflows/deploy.yml
@@ -169,6 +169,7 @@ jobs:
# otherwise its root-absolute asset URLs (/sandbox/_next/...) drop the
# /<repo> segment and 404. Derived from the repo name so it survives renames.
SANDBOX_BASE_PATH: /${{ github.event.repository.name }}/sandbox
+ SANDBOX_TEMPLATE_ASSETS_BASE_PATH: /${{ github.event.repository.name }}/sandbox/template-assets
NODE_OPTIONS: '--max-old-space-size=4096'
# The landing page (assembled in `deploy`) links these straight out of
diff --git a/.github/workflows/redeploy-preview.yml b/.github/workflows/redeploy-preview.yml
index 2066cfce4d..acd9f94cdd 100644
--- a/.github/workflows/redeploy-preview.yml
+++ b/.github/workflows/redeploy-preview.yml
@@ -124,7 +124,9 @@ jobs:
mkdir -p /tmp/gh-pages/pr/${PR_NUMBER}
cp -r storybook-dist/* /tmp/gh-pages/pr/${PR_NUMBER}/
mkdir -p /tmp/gh-pages/pr/${PR_NUMBER}/sandbox
- cp -r sandbox-dist/* /tmp/gh-pages/pr/${PR_NUMBER}/sandbox/
+ # Every preview points at the one shared main-sandbox asset copy.
+ find sandbox-dist -mindepth 1 -maxdepth 1 ! -name template-assets \
+ -exec cp -r {} /tmp/gh-pages/pr/${PR_NUMBER}/sandbox/ \;
cd /tmp/gh-pages
git add -A
diff --git a/apps/sandbox/babel.config.js b/apps/sandbox/babel.config.js
index 8fd233d19e..03175a37f2 100644
--- a/apps/sandbox/babel.config.js
+++ b/apps/sandbox/babel.config.js
@@ -6,7 +6,17 @@ const {babel} = require('@astryxdesign/build');
// Use the dist build pattern: library files keep default 'x' prefix (matching
// the pre-built astryx.css), product files use 'p' prefix to avoid collisions.
-module.exports = babel(path.resolve(__dirname, '../..'), {
+const config = babel(path.resolve(__dirname, '../..'), {
libraryPrefix: 'x',
classNamePrefix: 'p',
});
+
+// Template source stays portable with /template-assets/* URLs. Hosted sandbox
+// builds rewrite only executable string literals to their deployed asset path;
+// sourceRegistry's embedded code samples remain unchanged.
+config.plugins.push([
+ path.resolve(__dirname, 'scripts/rewrite-template-asset-paths.cjs'),
+ {basePath: process.env.SANDBOX_TEMPLATE_ASSETS_BASE_PATH || ''},
+]);
+
+module.exports = config;
diff --git a/apps/sandbox/scripts/rewrite-template-asset-paths.cjs b/apps/sandbox/scripts/rewrite-template-asset-paths.cjs
new file mode 100644
index 0000000000..9e6fa6442c
--- /dev/null
+++ b/apps/sandbox/scripts/rewrite-template-asset-paths.cjs
@@ -0,0 +1,25 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use strict';
+
+/* global module */
+module.exports = function rewriteTemplateAssetPaths({types: t}, options) {
+ const basePath = options.basePath || '';
+
+ return {
+ visitor: {
+ StringLiteral(path) {
+ if (
+ basePath !== '' &&
+ path.node.value.startsWith('/template-assets/')
+ ) {
+ path.replaceWith(
+ t.stringLiteral(
+ basePath + path.node.value.slice('/template-assets'.length),
+ ),
+ );
+ }
+ },
+ },
+ };
+};
diff --git a/scripts/rewrite-template-asset-paths.test.mjs b/scripts/rewrite-template-asset-paths.test.mjs
new file mode 100644
index 0000000000..55fed2a35a
--- /dev/null
+++ b/scripts/rewrite-template-asset-paths.test.mjs
@@ -0,0 +1,40 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+import {transformSync} from '@babel/core';
+import {describe, expect, it} from 'vitest';
+
+import rewriteTemplateAssetPaths from '../apps/sandbox/scripts/rewrite-template-asset-paths.cjs';
+
+function transform(source, basePath = '/astryx/sandbox/template-assets') {
+ return transformSync(source, {
+ configFile: false,
+ plugins: [[rewriteTemplateAssetPaths, {basePath}]],
+ }).code;
+}
+
+describe('rewriteTemplateAssetPaths', () => {
+ it('points template media at the configured shared path', () => {
+ const output = transform(`
+ const image = '/template-assets/image.png';
+ const untouched = '/images/image.png';
+ `);
+
+ expect(output).toContain('/astryx/sandbox/template-assets/image.png');
+ expect(output).toContain('/images/image.png');
+ });
+
+ it('leaves embedded source examples portable', () => {
+ const output = transform(
+ `const source = "const image = '/template-assets/image.png';";`,
+ );
+
+ expect(output).toContain('/template-assets/image.png');
+ expect(output).not.toContain('/astryx/sandbox/template-assets/image.png');
+ });
+
+ it('is a no-op without a configured path', () => {
+ expect(
+ transform(`const image = '/template-assets/image.png';`, ''),
+ ).toContain('/template-assets/image.png');
+ });
+});