Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Don't fetch stories.json (either JSON or SSE) if we don't need it #16318

Merged
merged 4 commits into from
Oct 13, 2021
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
12 changes: 0 additions & 12 deletions addons/storyshots/storyshots-core/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,13 @@ function callTestMethodGlobals(

const isDisabled = (parameter: any) =>
parameter === false || (parameter && parameter.disable === true);

// This is just here so that an error isn't thrown when we subclass `EventSource` in `StoryIndexClient`
// Currently the v7 store (that uses the client) does not work with Storyshots.
class EventSourceStandin {
constructor() {
throw new Error('EventSourceStandin is not intended to be used');
}
}

function testStorySnapshots(options: StoryshotsOptions = {}) {
if (typeof describe !== 'function') {
throw new Error('testStorySnapshots is intended only to be used inside jest');
}

addons.setChannel(mockChannel());

// Add a mock EventSource class as it is extended by the `StoryIndexClient` (we don't actually use that in v6 mode)
if (!global.EventSource) global.EventSource = EventSourceStandin;

const { storybook, framework, renderTree, renderShallowTree } = loadFramework(options);
const {
asyncJest,
Expand Down
16 changes: 11 additions & 5 deletions lib/api/src/lib/StoryIndexClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ import global from 'global';

import { StoryIndex } from './stories';

const { fetch } = global;
const { window, fetch, CONFIG_TYPE } = global;

const PATH = './stories.json';

// The stories.json endpoint both serves the basic data on a `GET` request and a stream of
// invalidation events when called as a `event-stream` (i.e. via SSE).
// So the `StoryIndexClient` is a EventSource that can also do a fetch
export class StoryIndexClient {
source: EventSource;

// eslint-disable-next-line no-undef
export class StoryIndexClient extends EventSource {
constructor() {
super(PATH);
if (CONFIG_TYPE === 'DEVELOPMENT') {
this.source = new window.EventSource(PATH);
}
}

// Silently never emit events in built storybook modes
addEventListener(event: string, cb: (...args: any[]) => void) {
if (this.source) this.source.addEventListener(event, cb);
}

async fetch() {
Expand Down
10 changes: 6 additions & 4 deletions lib/api/src/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { Args, ModuleFn } from '../index';
import { ComposedRef } from './refs';
import { StoryIndexClient } from '../lib/StoryIndexClient';

const { DOCS_MODE } = global;
const { DOCS_MODE, FEATURES } = global;
const INVALIDATE = 'INVALIDATE';

type Direction = -1 | 1;
Expand Down Expand Up @@ -502,9 +502,11 @@ export const init: ModuleFn = ({
}
);

indexClient = new StoryIndexClient();
indexClient.addEventListener(INVALIDATE, () => fullAPI.fetchStoryList());
await fullAPI.fetchStoryList();
if (FEATURES.storyStoreV7) {
indexClient = new StoryIndexClient();
indexClient.addEventListener(INVALIDATE, () => fullAPI.fetchStoryList());
await fullAPI.fetchStoryList();
}
};

return {
Expand Down
2 changes: 2 additions & 0 deletions lib/api/src/tests/stories.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jest.mock('../lib/events');
jest.mock('global', () => ({
...jest.requireActual('global'),
fetch: jest.fn(() => ({ json: () => ({ v: 3, stories: mockStories() }) })),
FEATURES: { storyStoreV7: true },
CONFIG_TYPE: 'DEVELOPMENT',
}));

beforeEach(() => {
Expand Down
1 change: 1 addition & 0 deletions lib/builder-webpack4/src/preview/iframe-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export default async (options: Options & Record<string, any>): Promise<Configura
options: templateOptions,
version: packageJson.version,
globals: {
CONFIG_TYPE: configType,
LOGLEVEL: logLevel,
FRAMEWORK_OPTIONS: frameworkOptions,
FEATURES: features,
Expand Down
1 change: 1 addition & 0 deletions lib/builder-webpack5/src/preview/iframe-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export default async (options: Options & Record<string, any>): Promise<Configura
options: templateOptions,
version: packageJson.version,
globals: {
CONFIG_TYPE: configType,
LOGLEVEL: logLevel,
FRAMEWORK_OPTIONS: frameworkOptions,
FEATURES: features,
Expand Down
2 changes: 2 additions & 0 deletions lib/manager-webpack4/src/presets/manager-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function managerWebpack(
releaseNotesData,
presets,
modern,
features,
}: Options & ManagerWebpackOptions
): Promise<Configuration> {
const envs = await presets.apply<Record<string, string>>('env');
Expand Down Expand Up @@ -99,6 +100,7 @@ export async function managerWebpack(
globals: {
CONFIG_TYPE: configType,
LOGLEVEL: logLevel,
FEATURES: features,
VERSIONCHECK: JSON.stringify(versionCheck),
RELEASE_NOTES_DATA: JSON.stringify(releaseNotesData),
DOCS_MODE: docsMode, // global docs mode
Expand Down
2 changes: 2 additions & 0 deletions lib/manager-webpack5/src/presets/manager-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function managerWebpack(
releaseNotesData,
presets,
modern,
features,
}: Options & ManagerWebpackOptions
): Promise<Configuration> {
const envs = await presets.apply<Record<string, string>>('env');
Expand Down Expand Up @@ -98,6 +99,7 @@ export async function managerWebpack(
globals: {
CONFIG_TYPE: configType,
LOGLEVEL: logLevel,
FEATURES: features,
VERSIONCHECK: JSON.stringify(versionCheck),
RELEASE_NOTES_DATA: JSON.stringify(releaseNotesData),
DOCS_MODE: docsMode, // global docs mode
Expand Down
17 changes: 12 additions & 5 deletions lib/preview-web/src/StoryIndexClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import global from 'global';

import { StoryIndex } from '@storybook/store';

const { fetch } = global;
const { window, fetch, CONFIG_TYPE } = global;

const PATH = './stories.json';

// The stories.json endpoint both serves the basic data on a `GET` request and a stream of
// invalidation events when called as a `event-stream` (i.e. via SSE).
// So the `StoryIndexClient` is a EventSource that can also do a fetch
export class StoryIndexClient {
source: EventSource;

// eslint-disable-next-line no-undef
export class StoryIndexClient extends EventSource {
constructor() {
super(PATH);
if (CONFIG_TYPE === 'DEVELOPMENT') {
this.source = new window.EventSource(PATH);
}
}

// Silently never emit events in bult storybook modes
addEventListener(event: string, cb: (...args: any[]) => void) {
if (this.source) this.source.addEventListener(event, cb);
}

async fetch() {
Expand Down