Skip to content

Commit 3a7c754

Browse files
committed
Fix cloudflare/hydrogen behaviour
1 parent 8886ddc commit 3a7c754

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

dev-packages/e2e-tests/test-applications/create-remix-app-v2-non-vite/tests/server-transactions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test('Sends two linked transactions (server & client) to Sentry', async ({ page
4747
const pageLoadParentSpanId = pageloadTransaction.contexts?.trace?.parent_span_id;
4848

4949
expect(httpServerTransaction.transaction).toBe('GET http://localhost:3030/');
50-
expect(pageloadTransaction.transaction).toBe('/');
50+
expect(pageloadTransaction.transaction).toBe('routes/_index');
5151

5252
expect(httpServerTraceId).toBeDefined();
5353
expect(httpServerSpanId).toBeDefined();

packages/remix/src/config/vite.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ export function sentryRemixVitePlugin(options: SentryRemixVitePluginOptions = {}
120120
/entry[.-]server\.[jt]sx?$/.test(id) ||
121121
// Also handle Remix's default entry.server location
122122
id.includes('/entry.server.') ||
123-
id.includes('/entry-server.');
123+
id.includes('/entry-server.') ||
124+
// Also handle Hydrogen/Cloudflare Workers server files
125+
/(^|\/)server\.[jt]sx?$/.test(id);
124126

125127
if (isClientEntry) {
126128
// XSS Prevention: Double-stringify strategy (same as transformIndexHtml above)
@@ -139,10 +141,11 @@ ${code}`;
139141

140142
if (isServerEntry) {
141143
// Inject into server entry for server-side transaction naming
144+
// Use globalThis for Cloudflare Workers/Hydrogen compatibility
142145
const injectedCode = `
143146
// Sentry Remix Route Manifest - Auto-injected
144-
if (typeof global !== 'undefined') {
145-
global.${MANIFEST_GLOBAL_KEY} = global.${MANIFEST_GLOBAL_KEY} || ${JSON.stringify(routeManifestJson)};
147+
if (typeof globalThis !== 'undefined') {
148+
globalThis.${MANIFEST_GLOBAL_KEY} = globalThis.${MANIFEST_GLOBAL_KEY} || ${JSON.stringify(routeManifestJson)};
146149
}
147150
${code}`;
148151

packages/remix/test/config/vite.test.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,9 @@ describe('sentryRemixVitePlugin', () => {
370370
const result = plugin.transform(code, id);
371371

372372
expect(result).not.toBeNull();
373-
expect(result?.code).toContain('global._sentryRemixRouteManifest');
373+
expect(result?.code).toContain('globalThis._sentryRemixRouteManifest');
374374
expect(result?.code).toContain(code);
375-
expect(result?.code).toContain("typeof global !== 'undefined'");
375+
expect(result?.code).toContain("typeof globalThis !== 'undefined'");
376376
});
377377

378378
it('should handle files with "entry.client" in path', () => {
@@ -526,7 +526,7 @@ describe('sentryRemixVitePlugin', () => {
526526
const result = plugin.transform(code, id);
527527

528528
expect(result).not.toBeNull();
529-
expect(result?.code).toContain('global._sentryRemixRouteManifest');
529+
expect(result?.code).toContain('globalThis._sentryRemixRouteManifest');
530530
});
531531

532532
it('should handle files with "entry.server" in path', () => {
@@ -550,7 +550,36 @@ describe('sentryRemixVitePlugin', () => {
550550
const result = plugin.transform(code, id);
551551

552552
expect(result).not.toBeNull();
553-
expect(result?.code).toContain('global._sentryRemixRouteManifest');
553+
expect(result?.code).toContain('globalThis._sentryRemixRouteManifest');
554+
});
555+
556+
it('should inject manifest into Hydrogen/Cloudflare server.ts files', () => {
557+
fs.writeFileSync(path.join(routesDir, 'index.tsx'), '// index');
558+
559+
const plugin = sentryRemixVitePlugin() as Plugin & {
560+
configResolved: (config: ResolvedConfig) => void;
561+
transform: (code: string, id: string) => { code: string; map: null } | null;
562+
};
563+
564+
const mockConfig: Partial<ResolvedConfig> = {
565+
root: tempDir,
566+
command: 'build',
567+
mode: 'production',
568+
};
569+
570+
plugin.configResolved(mockConfig as ResolvedConfig);
571+
572+
const code = 'console.log("Hydrogen server");';
573+
574+
// Test various server.ts paths
575+
const paths = ['/app/server.ts', 'server.ts', '/Users/project/server.ts'];
576+
577+
paths.forEach(id => {
578+
const result = plugin.transform(code, id);
579+
expect(result).not.toBeNull();
580+
expect(result?.code).toContain('globalThis._sentryRemixRouteManifest');
581+
expect(result?.code).toContain('console.log("Hydrogen server");');
582+
});
554583
});
555584
});
556585
});

0 commit comments

Comments
 (0)