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

Bl route warnings #2613

Merged
merged 5 commits into from
Oct 22, 2024
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
5 changes: 5 additions & 0 deletions .changeset/quick-carrots-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-hydrogen': patch
---

Add warnings to the Shopify CLI when your app uses reserved routes. These routes are reserved by Oxygen, and any local routes that conflict with them will not be used.
10 changes: 9 additions & 1 deletion packages/cli/src/commands/hydrogen/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {commonFlags, flagsToCamelObject} from '../../lib/flags.js';
import {prepareDiffDirectory} from '../../lib/template-diff.js';
import {getViteConfig, isViteProject} from '../../lib/vite-config.js';
import {checkLockfileStatus} from '../../lib/check-lockfile.js';
import {findMissingRoutes} from '../../lib/missing-routes.js';
import {
findMissingRoutes,
findReservedRoutes,
warnReservedRoutes,
} from '../../lib/route-validator.js';
import {runClassicCompilerBuild} from '../../lib/classic-compiler/build.js';
import {hydrogenBundleAnalyzer} from '../../lib/bundle/vite-plugin.js';
import {
Expand Down Expand Up @@ -341,6 +345,10 @@ export async function runBuild({
}
}

if (!watch && !disableRouteWarning) {
warnReservedRoutes(findReservedRoutes(remixConfig));
}

return {
async close() {
codegenProcess?.removeAllListeners('close');
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/hydrogen/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import Command from '@shopify/cli-kit/node/base-command';
import {resolvePath} from '@shopify/cli-kit/node/path';
import {commonFlags} from '../../lib/flags.js';
import {getRemixConfig} from '../../lib/remix-config.js';
import {findMissingRoutes, logMissingRoutes} from '../../lib/missing-routes.js';
import {
findMissingRoutes,
findReservedRoutes,
logMissingRoutes,
warnReservedRoutes,
} from '../../lib/route-validator.js';

import {Args} from '@oclif/core';

Expand Down Expand Up @@ -40,4 +45,5 @@ export default class GenerateRoute extends Command {
export async function runCheckRoutes({directory}: {directory: string}) {
const remixConfig = await getRemixConfig(directory);
logMissingRoutes(findMissingRoutes(remixConfig));
warnReservedRoutes(findReservedRoutes(remixConfig));
}
2 changes: 1 addition & 1 deletion packages/cli/src/lib/classic-compiler/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
type ServerMode,
} from '../remix-config.js';
import {checkLockfileStatus} from '../check-lockfile.js';
import {findMissingRoutes} from '../missing-routes.js';
import {findMissingRoutes} from '../route-validator.js';
import {createRemixLogger, muteRemixLogs} from '../log.js';
import {codegen} from '../codegen.js';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, it, expect} from 'vitest';
import {findMissingRoutes} from './missing-routes.js';
import {findMissingRoutes, findReservedRoutes} from './route-validator.js';

const createRoute = (path: string) => ({
routes: {
Expand Down Expand Up @@ -50,3 +50,41 @@ describe('missing-routes', () => {
}
});
});

describe('reserved-routes', () => {
it('returns an empty array when no routes are present', async () => {
expect(findReservedRoutes({routes: {}})).toHaveLength(0);
});

it("doesn't find routes that don't match the reserved routes", async () => {
expect(findReservedRoutes(createRoute('collections/:handle'))).toHaveLength(
0,
);
});

it('returns an array of reserved routes', async () => {
expect(
findReservedRoutes(createRoute('api/2024-10/graphql.json')),
).toHaveLength(1);

expect(
findReservedRoutes(createRoute('api/:param/graphql.json')),
).toHaveLength(1);
});

it('finds reserved routes /cdn/', async () => {
expect(findReservedRoutes(createRoute('cdn/'))).toHaveLength(1);

expect(
findReservedRoutes(createRoute('cdn/something/for/you.jpg')),
).toHaveLength(1);
});

it('finds reserved routes /_t/', async () => {
expect(findReservedRoutes(createRoute('_t/'))).toHaveLength(1);

expect(
findReservedRoutes(createRoute('_t/something/for/you.jpg')),
).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import {renderSuccess, renderWarning} from '@shopify/cli-kit/node/ui';
import type {RemixConfig} from './remix-config.js';

const RESERVED_ROUTES = ['^api/[^/]+/graphql.json', '^cdn/', '^_t/'];

export function findReservedRoutes(config: {routes: RemixConfig['routes']}) {
const routes = new Set<string>();

Object.values(config.routes)
.filter((route) =>
RESERVED_ROUTES.some((pattern) =>
new RegExp(pattern).test(route.path ?? ''),
),
)
.forEach((route) => {
if (route.path) {
routes.add(route.path);
}
});

return [...routes];
}

// Sorted by importance for better warnings.
const REQUIRED_ROUTES = [
'',
Expand Down Expand Up @@ -117,3 +137,23 @@ export function logMissingRoutes(routes: string[]) {
});
}
}

export function warnReservedRoutes(routes: string[]) {
if (routes.length) {
renderWarning({
headline: 'Reserved routes present',
body:
`Your Hydrogen project is using ${routes.length} reserved route${
routes.length > 1 ? 's' : ''
}.\n` +
'These routes are reserved by Shopify and may cause issues with your storefront:\n\n' +
routes
.slice(0, LINE_LIMIT - (routes.length <= LINE_LIMIT ? 0 : 1))
.map((route) => `• /${route}`)
.join('\n') +
(routes.length > LINE_LIMIT
? `\n• ...and ${routes.length - LINE_LIMIT + 1} more`
: ''),
});
}
}
Loading