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
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ export const isViteServer = !!process.env.E2E_TEST_VITE_SERVER;
export const NITRO_ONLY = 'Nitro only: the Vite server builds no `server/` directory';

export const NO_IMPORT_CONFIG = 'Nitro only: the Vite server never had an `--import` config to keep working';

export const NO_META_TAGS = 'Vite server: the SDK adds no trace meta tags yet';
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import { expect, test, type Page } from '@playwright/test';
import { PAGE_SERVER_RENDERING_TYPE, SENTRY_LINK_TYPE } from '@sentry/conventions/attributes';
import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils';
import { isViteServer, NO_META_TAGS } from './isViteServer';
import { isViteServer } from './isViteServer';

// The Vite server neither caches nor prerenders routes in `nuxt build`, so these pages render for each request.
const testCachedPage = isViteServer ? testChangingTracingMetaTagsOnISRPage : testCacheOriginOnRuntimeCachedPage;
const testPrerenderedPage = isViteServer ? testChangingTracingMetaTagsOnISRPage : testEmptyCacheOriginOnPrerenderedPage;

test.describe('Rendering Modes with Cached HTML', () => {
test('changes tracing meta tags with multiple requests on Client-Side only page', async ({ page }) => {
test.fail(isViteServer, NO_META_TAGS);
await testChangingTracingMetaTagsOnISRPage(page, '/rendering-modes/client-side-only-page', 'Client Side Only Page');
});

test('changes tracing meta tags with multiple requests on ISR-cached page', async ({ page }) => {
test.fail(isViteServer, NO_META_TAGS);
await testChangingTracingMetaTagsOnISRPage(page, '/rendering-modes/isr-cached-page', 'ISR Cached Page');
});

test('changes tracing meta tags with multiple requests on 1h ISR-cached page', async ({ page }) => {
test.fail(isViteServer, NO_META_TAGS);
await testChangingTracingMetaTagsOnISRPage(page, '/rendering-modes/isr-1h-cached-page', 'ISR 1h Cached Page');
});

test('links the render as cache origin on SWR-cached page', async ({ page }) => {
test.fail(isViteServer, NO_META_TAGS);
await testCacheOriginOnRuntimeCachedPage(page, '/rendering-modes/swr-cached-page', 'SWR Cached Page');
await testCachedPage(page, '/rendering-modes/swr-cached-page', 'SWR Cached Page');
});

test('links the render as cache origin on SWR 1h cached page', async ({ page }) => {
test.fail(isViteServer, NO_META_TAGS);
await testCacheOriginOnRuntimeCachedPage(page, '/rendering-modes/swr-1h-cached-page', 'SWR 1h Cached Page');
await testCachedPage(page, '/rendering-modes/swr-1h-cached-page', 'SWR 1h Cached Page');
});

test('marks pageloads of pre-rendered page as static without a cache origin link', async ({ page }) => {
test.fail(isViteServer, NO_META_TAGS);
await testEmptyCacheOriginOnPrerenderedPage(page, '/rendering-modes/pre-rendered-page', 'Pre-Rendered Page');
await testPrerenderedPage(page, '/rendering-modes/pre-rendered-page', 'Pre-Rendered Page');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { APIRequestContext } from '@playwright/test';
import { expect, test } from '@playwright/test';
import { collectStreamedSpans, getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils';
import { IMPORT_SURFACES } from './importSurfaces';
import { isViteServer, NITRO_ONLY, NO_META_TAGS } from './isViteServer';
import { isViteServer, NITRO_ONLY } from './isViteServer';
import { RUNTIME, SERVER_ORIGIN } from './runtime';

// `sentryCloudflareNitroPlugin` takes the trace data of the meta tags when the request starts, before the route
Expand All @@ -25,7 +25,6 @@ test.describe('distributed tracing', () => {
const PARAM = 's0me-param';

test('puts the parametrized route in the baggage meta tag', async ({ request }) => {
test.fail(isViteServer, NO_META_TAGS);
test.skip(RUNTIME === 'cloudflare', CLOUDFLARE_META_TAGS_BEFORE_ROUTE);

const { baggageMetaTagContent, serverSpan } = await getBaggageMetaTagAndServerSpan(request, `/test-param/${PARAM}`);
Expand All @@ -43,8 +42,6 @@ test.describe('distributed tracing', () => {
});

test('capture a distributed pageload trace', async ({ page }) => {
test.fail(isViteServer, NO_META_TAGS);

const clientSpanPromise = waitForStreamedSpan('nuxt-4', span => {
return getSpanOp(span) === 'pageload' && span.is_segment;
});
Expand Down
7 changes: 7 additions & 0 deletions packages/nuxt/src/runtime/vite-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useServerHooks } from 'nuxt/server';
import { addSentryTracingMetaTags } from './utils';

// The Vite server has no route cache, so every page gets the trace of its own request.
useServerHooks().hook('render:html', html => {
addSentryTracingMetaTags(html.head);
});
7 changes: 4 additions & 3 deletions packages/nuxt/src/vite/viteServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const SERVER_ENTRY_ALIAS = '#server-entry';

/**
* Sets up the server SDK on the Vite server (`server.builder: 'vite'`), which runs no Nitro. The
* `ssr` build imports the Sentry server config at the top of the server entry, and orchestrion
* instruments the dependencies in that build.
* `ssr` build imports the Sentry server config and the meta tag hook at the top of the server entry,
* and orchestrion instruments the dependencies in that build.
*/
export function setupViteServer(nuxt: Nuxt, serverConfigFile: string, moduleOptions: SentryNuxtModuleOptions): void {
// `nuxt prepare` (often a `postinstall` script) only generates types, so it needs no plugins and no warnings.
Expand All @@ -29,7 +29,8 @@ export function setupViteServer(nuxt: Nuxt, serverConfigFile: string, moduleOpti
}

const configPath = createResolver(nuxt.options.rootDir).resolve(serverConfigFile);
const importPaths = [addRuntimeFlagsTemplate().dst, configPath];
const metaTagsPath = createResolver(import.meta.url).resolve('./runtime/vite-server');
const importPaths = [addRuntimeFlagsTemplate().dst, configPath, metaTagsPath];
let isConfigBundled = false;

addVitePlugin(
Expand Down
6 changes: 4 additions & 2 deletions packages/nuxt/test/vite/viteServer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Nuxt } from '@nuxt/schema';
import { pathToFileURL } from 'node:url';
import type { Plugin } from 'vite';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { setupViteServer } from '../../src/vite/viteServer';
Expand All @@ -19,6 +20,7 @@ vi.mock('@sentry/server-utils/orchestrion/vite', () => ({
const APP_ROOT = '/my/app';
const FLAGS_PATH = `${APP_ROOT}/.nuxt/sentry-runtime-flags.mjs`;
const CONFIG_PATH = `${APP_ROOT}/sentry.server.config.ts`;
const META_TAGS_PATH = `${pathToFileURL('src/vite/viteServer.ts').href}/./runtime/vite-server`;
const SERVER_ENTRY_ID = 'virtual:nuxt:.nuxt%2Fvite-server%2Fserver-handler.mjs';
const EXPERIMENTAL_WARNING =
"[Sentry] Support for the Nuxt Vite server (`server.builder: 'vite'`) is experimental and can change in a minor release. The server SDK starts only in production builds that run on Node.js.";
Expand Down Expand Up @@ -82,15 +84,15 @@ describe('setupViteServer', () => {
});
});

it('imports the runtime flags and the config from the project root into the server entry', async () => {
it('imports the runtime flags, the config from the project root and the meta tag hook into the server entry', async () => {
vi.spyOn(console, 'warn').mockImplementation(() => {});
setupViteServer(createFakeNuxt().nuxt, 'sentry.server.config.ts', {});
const transform = await buildServerEntryPlugin(SERVER_ENTRY_ID);

const result = transform('export default {};', SERVER_ENTRY_ID);

expect(result).toEqual({
code: `import "${FLAGS_PATH}";import "${CONFIG_PATH}";export default {};`,
code: `import "${FLAGS_PATH}";import "${CONFIG_PATH}";import "${META_TAGS_PATH}";export default {};`,
map: null,
});
});
Expand Down
Loading