Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-sull authored and astrobot-houston committed Jul 29, 2022
1 parent a0d1731 commit 2ac5c55
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 87 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/core/routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { createRouteManifest } from './manifest/create.js';
export { deserializeRouteData, serializeRouteData } from './manifest/serialization.js';
export { matchRoute, matchAllRoutes } from './match.js';
export { matchAllRoutes, matchRoute } from './match.js';
export { getParams } from './params.js';
export { validateGetStaticPathsModule, validateGetStaticPathsResult } from './validation.js';
137 changes: 71 additions & 66 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { AstroConfig, InjectedRoute, ManifestData, RouteData, RoutePart } from '../../../@types/astro';
import type {
AstroConfig,
InjectedRoute,
ManifestData,
RouteData,
RoutePart,
} from '../../../@types/astro';
import type { LogOptions } from '../../logger/core';

import fs from 'fs';
Expand Down Expand Up @@ -179,7 +185,7 @@ function injectedRouteToItem(
isIndex: true,
isPage,
routeSuffix: pattern.slice(pattern.indexOf('.'), -ext.length),
}
};
}

/** Create manifest of all static routes */
Expand Down Expand Up @@ -311,75 +317,74 @@ export function createRouteManifest(
warn(logging, 'astro', `Missing pages directory: ${pagesDirRootRelative}`);
}

config?._ctx?.injectedRoutes?.sort((a, b) =>
// sort injected routes in the same way as user-defined routes
comparator(
injectedRouteToItem({ config, cwd }, a),
injectedRouteToItem({ config, cwd}, b)
))
config?._ctx?.injectedRoutes
?.sort((a, b) =>
// sort injected routes in the same way as user-defined routes
comparator(injectedRouteToItem({ config, cwd }, a), injectedRouteToItem({ config, cwd }, b))
)
.reverse() // prepend to the routes array from lowest to highest priority
.forEach(({ pattern: name, entryPoint }) => {
const resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));

const isDynamic = (str: string) => str?.[0] === '[';
const normalize = (str: string) => str?.substring(1, str?.length - 1);

const segments = removeLeadingForwardSlash(name)
.split(path.sep)
.filter(Boolean)
.map((s: string) => {
validateSegment(s);

const dynamic = isDynamic(s);
const content = dynamic ? normalize(s) : s;
return [
{
content,
dynamic,
spread: isSpread(s),
},
];
});
const resolved = require.resolve(entryPoint, { paths: [cwd || fileURLToPath(config.root)] });
const component = slash(path.relative(cwd || fileURLToPath(config.root), resolved));

const isDynamic = (str: string) => str?.[0] === '[';
const normalize = (str: string) => str?.substring(1, str?.length - 1);

const segments = removeLeadingForwardSlash(name)
.split(path.sep)
.filter(Boolean)
.map((s: string) => {
validateSegment(s);

const dynamic = isDynamic(s);
const content = dynamic ? normalize(s) : s;
return [
{
content,
dynamic,
spread: isSpread(s),
},
];
});

const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
const isPage = type === 'page';
const trailingSlash = isPage ? config.trailingSlash : 'never';

const pattern = getPattern(segments, trailingSlash);
const generate = getRouteGenerator(segments, trailingSlash);
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
: null;
const params = segments
.flat()
.filter((p) => p.dynamic)
.map((p) => p.content);
const route = `/${segments
.map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content))
.join('/')}`.toLowerCase();

const collision = routes.find(({ route: r }) => r === route);
if (collision) {
throw new Error(
`An integration attempted to inject a route that is already used in your project: "${route}" at "${component}". \nThis route collides with: "${collision.component}".`
);
}
const type = resolved.endsWith('.astro') ? 'page' : 'endpoint';
const isPage = type === 'page';
const trailingSlash = isPage ? config.trailingSlash : 'never';

const pattern = getPattern(segments, trailingSlash);
const generate = getRouteGenerator(segments, trailingSlash);
const pathname = segments.every((segment) => segment.length === 1 && !segment[0].dynamic)
? `/${segments.map((segment) => segment[0].content).join('/')}`
: null;
const params = segments
.flat()
.filter((p) => p.dynamic)
.map((p) => p.content);
const route = `/${segments
.map(([{ dynamic, content }]) => (dynamic ? `[${content}]` : content))
.join('/')}`.toLowerCase();

const collision = routes.find(({ route: r }) => r === route);
if (collision) {
throw new Error(
`An integration attempted to inject a route that is already used in your project: "${route}" at "${component}". \nThis route collides with: "${collision.component}".`
);
}

// the routes array was already sorted by priority,
// pushing to the front of the list ensure that injected routes
// are given priority over all user-provided routes
routes.unshift({
type,
route,
pattern,
segments,
params,
component,
generate,
pathname: pathname || void 0,
// the routes array was already sorted by priority,
// pushing to the front of the list ensure that injected routes
// are given priority over all user-provided routes
routes.unshift({
type,
route,
pattern,
segments,
params,
component,
generate,
pathname: pathname || void 0,
});
});
});

return {
routes,
Expand Down
12 changes: 5 additions & 7 deletions packages/astro/src/vite-plugin-astro-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/core.j
import { preload, ssr } from '../core/render/dev/index.js';
import { RouteCache } from '../core/render/route-cache.js';
import { createRequest } from '../core/request.js';
import { createRouteManifest, matchAllRoutes, matchRoute } from '../core/routing/index.js';
import { createRouteManifest, matchAllRoutes } from '../core/routing/index.js';
import { createSafeError, resolvePages } from '../core/util.js';
import notFoundTemplate, { subpathNotUsedTemplate } from '../template/4xx.js';

Expand Down Expand Up @@ -254,9 +254,7 @@ async function handleRequest(
if (config.output === 'server' && matches.length > 1) {
throw new Error(`Found multiple matching routes for "${pathname}"! When using \`output: 'server'\`, only one route in \`src/pages\` can match a given URL. Found:
${
matches.map(({ component }) => `- ${component}`).join('\n')
}
${matches.map(({ component }) => `- ${component}`).join('\n')}
`);
}

Expand All @@ -281,7 +279,7 @@ ${
filePath,
preloadedComponent,
mod,
}
};
}
}

Expand All @@ -300,12 +298,12 @@ ${
const filePath = new URL(`./${custom404.component}`, config.root);
const preloadedComponent = await preload({ astroConfig: config, filePath, viteServer });
const [, mod] = preloadedComponent;

return {
route: custom404,
filePath,
preloadedComponent,
mod
mod,
};
}

Expand Down
8 changes: 4 additions & 4 deletions packages/astro/test/astro-get-static-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('getStaticPaths - dev calls', () => {
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;

fixture = await loadFixture({ root: './fixtures/astro-get-static-paths/' });
devServer = await fixture.startDevServer();
});
Expand All @@ -43,7 +43,7 @@ describe('getStaticPaths - dev calls', () => {

res = await fixture.fetch('/b');
expect(res.status).to.equal(200);

res = await fixture.fetch('/c');
expect(res.status).to.equal(200);
});
Expand All @@ -56,7 +56,7 @@ describe('getStaticPaths - 404 behavior', () => {
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;

fixture = await loadFixture({ root: './fixtures/astro-get-static-paths/' });
devServer = await fixture.startDevServer();
});
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('getStaticPaths - numeric route params', () => {
before(async () => {
// reset the flag used by [...calledTwiceTest].astro between each test
globalThis.isCalledOnce = false;

fixture = await loadFixture({
root: './fixtures/astro-get-static-paths/',
site: 'https://mysite.dev/',
Expand Down
20 changes: 11 additions & 9 deletions packages/astro/test/routing-priority.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,25 @@ const routes = [
{
description: 'matches /injected to to-inject.astro',
url: '/injected',
h1: 'to-inject.astro'
h1: 'to-inject.astro',
},
{
description: 'matches /_injected to to-inject.astro',
url: '/_injected',
h1: 'to-inject.astro'
h1: 'to-inject.astro',
},
{
description: 'matches /injected-1 to [id].astro',
url: '/injected-1',
h1: '[id].astro',
p: 'injected-1'
p: 'injected-1',
},
{
description: 'matches /injected-2 to [id].astro',
url: '/injected-2',
h1: '[id].astro',
p: 'injected-2'
}
p: 'injected-2',
},
];

function appendForwardSlash(path) {
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('Routing priority', () => {
it(description, async () => {
const html = await fixture.fetch(url).then((res) => res.text());
const $ = cheerioLoad(html);

expect($('h1').text()).to.equal(h1);

if (p) {
Expand All @@ -159,7 +159,7 @@ describe('Routing priority', () => {
it(`${description} (trailing slash)`, async () => {
const html = await fixture.fetch(appendForwardSlash(url)).then((res) => res.text());
const $ = cheerioLoad(html);

expect($('h1').text()).to.equal(h1);

if (p) {
Expand All @@ -169,9 +169,11 @@ describe('Routing priority', () => {

// checks with index.html, ex: '/de/index.html' instead of '/de'
it(`${description} (index.html)`, async () => {
const html = await fixture.fetch(`${appendForwardSlash(url)}index.html`).then((res) => res.text());
const html = await fixture
.fetch(`${appendForwardSlash(url)}index.html`)
.then((res) => res.text());
const $ = cheerioLoad(html);

expect($('h1').text()).to.equal(h1);

if (p) {
Expand Down

0 comments on commit 2ac5c55

Please sign in to comment.