Skip to content
Open
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
6 changes: 3 additions & 3 deletions static/app/stories/apiReference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Tooltip} from '@sentry/scraps/tooltip';

import {IconChevron} from 'sentry/icons';
import {IconSearch} from 'sentry/icons/iconSearch';
import * as Storybook from 'sentry/stories';
import {Section} from 'sentry/stories/layout';
import {fzf} from 'sentry/utils/search/fzf';

interface APIReferenceProps {
Expand All @@ -21,7 +21,7 @@ export function APIReference(props: APIReferenceProps) {
const nodes = usePropTree(props.componentProps?.props ?? {}, query);

return (
<Storybook.Section>
<Section>
{props.componentProps?.description && <p>{props.componentProps.description}</p>}
<Container marginBottom="md">
<InputGroup>
Expand Down Expand Up @@ -87,7 +87,7 @@ export function APIReference(props: APIReferenceProps) {
</tbody>
</StoryTypesTable>
</StoryTableContainer>
</Storybook.Section>
</Section>
);
}

Expand Down
16 changes: 10 additions & 6 deletions static/app/stories/storybook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import {Children, Fragment, useEffect} from 'react';
import {Container} from '@sentry/scraps/layout';
import {Heading} from '@sentry/scraps/text';

import {makeStorybookDocumentTitle} from 'sentry/stories/view/storyExports';
import {StoryHeading} from 'sentry/stories/view/storyHeading';

import * as Storybook from './';
import {APIReference} from './apiReference';
import {Section, SideBySide} from './layout';

function makeStorybookDocumentTitle(title: string | undefined): string {
return title ? `${title} — Scraps` : 'Scraps';
}

type StoryRenderFunction = () => ReactNode | ReactNode[];
type StoryContext = (storyName: string, story: StoryRenderFunction) => void;
Expand Down Expand Up @@ -47,7 +51,7 @@ export function story(title: string, setup: SetupFunction): StoryRenderFunction
<Story key={name + idx} name={name} render={render} />
))}
{APIDocumentation.map((documentation, i) => (
<Storybook.APIReference key={i} componentProps={documentation} />
<APIReference key={i} componentProps={documentation} />
))}
</Fragment>
);
Expand All @@ -59,13 +63,13 @@ function Story(props: {name: string; render: StoryRenderFunction}) {
const isOneChild = Children.count(children) === 1;

return (
<Storybook.Section>
<Section>
<Container borderBottom="primary">
<StoryHeading as="h2" size="2xl">
{props.name}
</StoryHeading>
</Container>
{isOneChild ? children : <Storybook.SideBySide>{children}</Storybook.SideBySide>}
</Storybook.Section>
{isOneChild ? children : <SideBySide>{children}</SideBySide>}
</Section>
);
}
2 changes: 1 addition & 1 deletion static/app/stories/view/storyExports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function StoryLayout() {
);
}

export function makeStorybookDocumentTitle(title: string | undefined): string {
function makeStorybookDocumentTitle(title: string | undefined): string {
return title ? `${title} — Scraps` : 'Scraps';
}

Expand Down
55 changes: 51 additions & 4 deletions static/app/stories/view/useStoriesLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
import type React from 'react';
import {useMemo} from 'react';
import {useMemo, useSyncExternalStore} from 'react';

import {useQuery, type UseQueryResult} from 'sentry/utils/queryClient';

const context = require.context('sentry', true, /\.stories.tsx$/, 'lazy');
const mdxContext = require.context('sentry', true, /\.mdx$/, 'lazy');
let context = import.meta.webpackContext('sentry', {
recursive: true,
regExp: /\.stories.tsx$/,
mode: 'lazy',
});
let mdxContext = import.meta.webpackContext('sentry', {
recursive: true,
regExp: /\.mdx$/,
mode: 'lazy',
});

// External store that increments whenever HMR replaces a story or mdx file.
// Accepting context module IDs creates proper HMR boundaries — without this,
// updates are silently dropped or cause "unexpected require from disposed module".
let _storiesHmrVersion = 0;
const _storiesHmrListeners = new Set<() => void>();

if (process.env.NODE_ENV === 'development' && import.meta.webpackHot) {
const onUpdate = () => {
// Re-capture and re-register after each replacement — the old context
// reference is stale and its replacement won't have accept handlers.
context = import.meta.webpackContext('sentry', {
recursive: true,
regExp: /\.stories.tsx$/,
mode: 'lazy',
});
mdxContext = import.meta.webpackContext('sentry', {
recursive: true,
regExp: /\.mdx$/,
mode: 'lazy',
});
import.meta.webpackHot!.accept(context.id as string, onUpdate);
import.meta.webpackHot!.accept(mdxContext.id as string, onUpdate);
_storiesHmrVersion++;
_storiesHmrListeners.forEach(l => l());
};
Comment on lines +31 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The useStoryBookFiles hook uses useMemo with an empty dependency array, causing it to return a stale list of stories after an HMR update.
Severity: MEDIUM

Suggested Fix

Add the hmrVersion variable to the dependency array of the useMemo call within the useStoryBookFiles hook. This will ensure the list of story files is re-calculated whenever an HMR update occurs, similar to the pattern used in useStoriesLoader.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: static/app/stories/view/useStoriesLoader.tsx#L24-L41

Potential issue: The `useStoryBookFiles` hook memoizes its result using `useMemo` with
an empty dependency array. During a Hot Module Replacement (HMR) update, the
module-level variables `context` and `mdxContext` are reassigned with new story data.
However, since the `useMemo` has no dependencies, it doesn't re-evaluate and continues
to return the cached, stale list of story files from before the HMR update. As a result,
any new or modified stories will not appear in the story list sidebar until a full page
refresh is performed, undermining the HMR functionality.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Member Author

@scttcper scttcper Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conflates two different scenarios: file modification (what HMR handles) vs. file addition/deletion (what requires a full rebuild).

The only scenario where useMemo([]) would matter is "I added a new story file and want it to appear without refreshing" — but that's not an HMR scenario,
it's a full rebuild. After the rebuild the component remounts anyway and the memo re-runs.

import.meta.webpackHot.accept(context.id as string, onUpdate);
import.meta.webpackHot.accept(mdxContext.id as string, onUpdate);
}

function subscribeToStoriesHmr(listener: () => void) {
_storiesHmrListeners.add(listener);
return () => _storiesHmrListeners.delete(listener);
}

function getStoriesHmrVersion() {
return _storiesHmrVersion;
}

export interface StoryResources {
a11y?: Record<string, string>;
Expand Down Expand Up @@ -78,8 +124,9 @@ interface UseStoriesLoaderOptions {
export function useStoriesLoader(
options: UseStoriesLoaderOptions
): UseQueryResult<StoryDescriptor[], Error> {
const hmrVersion = useSyncExternalStore(subscribeToStoriesHmr, getStoriesHmrVersion);
return useQuery({
queryKey: [options.files],
queryKey: [options.files, hmrVersion],
queryFn: (): Promise<StoryDescriptor[]> => {
return Promise.all(options.files.map(importStory));
},
Expand Down
Loading