mirror of
https://github.com/withastro/astro.git
synced 2025-03-17 23:11:29 -05:00
[ci] format
This commit is contained in:
parent
cb356a5db6
commit
cfbf428643
20 changed files with 66 additions and 63 deletions
|
@ -1678,14 +1678,14 @@ export interface AstroUserConfig {
|
|||
* @description
|
||||
*
|
||||
* When [`i18n.fallback`](#i18nfallback) is configured to avoid showing a 404 page for missing page routes, this option controls whether to [redirect](https://docs.astro.build/en/guides/routing/#redirects) to the fallback page, or to [rewrite](https://docs.astro.build/en/guides/routing/#rewrites) the fallback page's content in place.
|
||||
*
|
||||
*
|
||||
* By default, Astro's i18n routing creates pages that redirect your visitors to a new destination based on your fallback configuration. The browser will refresh and show the destination address in the URL bar.
|
||||
*
|
||||
* When `i18n.routing.fallback: "rewrite"` is configured, Astro will create pages that render the contents of the fallback page on the original, requested URL.
|
||||
*
|
||||
* With the following configuration, if you have the file `src/pages/en/about.astro` but not `src/pages/fr/about.astro`, the `astro build` command will generate `dist/fr/about.html` with the same content as the `dist/en/index.html` page.
|
||||
* Your site visitor will see the English version of the page at `https://example.com/fr/about/` and will not be redirected.
|
||||
*
|
||||
*
|
||||
* ```js
|
||||
* //astro.config.mjs
|
||||
* export default defineConfig({
|
||||
|
@ -1703,7 +1703,7 @@ export interface AstroUserConfig {
|
|||
* })
|
||||
* ```
|
||||
*/
|
||||
fallbackType: "redirect" | "rewrite"
|
||||
fallbackType: 'redirect' | 'rewrite';
|
||||
|
||||
/**
|
||||
* @name i18n.routing.strategy
|
||||
|
|
|
@ -5,7 +5,7 @@ import type {
|
|||
SSRElement,
|
||||
SSRResult,
|
||||
} from '../@types/astro.js';
|
||||
import {type HeadElements, Pipeline, type TryRewriteResult} from '../core/base-pipeline.js';
|
||||
import { type HeadElements, Pipeline, type TryRewriteResult } from '../core/base-pipeline.js';
|
||||
import type { SinglePageBuiltModule } from '../core/build/types.js';
|
||||
import {
|
||||
createModuleScriptElement,
|
||||
|
@ -68,11 +68,8 @@ export class ContainerPipeline extends Pipeline {
|
|||
return { links, styles, scripts };
|
||||
}
|
||||
|
||||
async tryRewrite(
|
||||
payload: RewritePayload,
|
||||
request: Request,
|
||||
): Promise<TryRewriteResult> {
|
||||
const {newUrl,pathname,routeData} = findRouteToRewrite({
|
||||
async tryRewrite(payload: RewritePayload, request: Request): Promise<TryRewriteResult> {
|
||||
const { newUrl, pathname, routeData } = findRouteToRewrite({
|
||||
payload,
|
||||
request,
|
||||
routes: this.manifest?.routes.map((r) => r.routeData),
|
||||
|
@ -82,7 +79,7 @@ export class ContainerPipeline extends Pipeline {
|
|||
});
|
||||
|
||||
const componentInstance = await this.getComponentByRoute(routeData);
|
||||
return {componentInstance, routeData, newUrl, pathname};
|
||||
return { componentInstance, routeData, newUrl, pathname };
|
||||
}
|
||||
|
||||
insertRoute(route: RouteData, componentInstance: ComponentInstance): void {
|
||||
|
|
|
@ -6,7 +6,7 @@ import type {
|
|||
SSRElement,
|
||||
SSRResult,
|
||||
} from '../../@types/astro.js';
|
||||
import {Pipeline, type TryRewriteResult} from '../base-pipeline.js';
|
||||
import { Pipeline, type TryRewriteResult } from '../base-pipeline.js';
|
||||
import type { SinglePageBuiltModule } from '../build/types.js';
|
||||
import { RedirectSinglePageBuiltModule } from '../redirects/component.js';
|
||||
import { createModuleScriptElement, createStylesheetElementSet } from '../render/ssr-element.js';
|
||||
|
@ -95,7 +95,7 @@ export class AppPipeline extends Pipeline {
|
|||
request: Request,
|
||||
_sourceRoute: RouteData,
|
||||
): Promise<TryRewriteResult> {
|
||||
const { newUrl,pathname,routeData} = findRouteToRewrite({
|
||||
const { newUrl, pathname, routeData } = findRouteToRewrite({
|
||||
payload,
|
||||
request,
|
||||
routes: this.manifest?.routes.map((r) => r.routeData),
|
||||
|
@ -105,7 +105,7 @@ export class AppPipeline extends Pipeline {
|
|||
});
|
||||
|
||||
const componentInstance = await this.getComponentByRoute(routeData);
|
||||
return {newUrl, pathname, componentInstance, routeData};
|
||||
return { newUrl, pathname, componentInstance, routeData };
|
||||
}
|
||||
|
||||
async getModuleForRoute(route: RouteData): Promise<SinglePageBuiltModule> {
|
||||
|
|
|
@ -76,7 +76,7 @@ export type SSRManifest = {
|
|||
|
||||
export type SSRManifestI18n = {
|
||||
fallback: Record<string, string> | undefined;
|
||||
fallbackType: "redirect" | "rewrite";
|
||||
fallbackType: 'redirect' | 'rewrite';
|
||||
strategy: RoutingStrategies;
|
||||
locales: Locales;
|
||||
defaultLocale: string;
|
||||
|
|
|
@ -108,8 +108,8 @@ export abstract class Pipeline {
|
|||
export interface HeadElements extends Pick<SSRResult, 'scripts' | 'styles' | 'links'> {}
|
||||
|
||||
export interface TryRewriteResult {
|
||||
routeData: RouteData,
|
||||
componentInstance: ComponentInstance,
|
||||
newUrl: URL,
|
||||
pathname: string
|
||||
routeData: RouteData;
|
||||
componentInstance: ComponentInstance;
|
||||
newUrl: URL;
|
||||
pathname: string;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ import {
|
|||
removeLeadingForwardSlash,
|
||||
removeTrailingForwardSlash,
|
||||
} from '../../core/path.js';
|
||||
import {toFallbackType, toRoutingStrategy} from '../../i18n/utils.js';
|
||||
import { toFallbackType, toRoutingStrategy } from '../../i18n/utils.js';
|
||||
import { runHookBuildGenerated } from '../../integrations/hooks.js';
|
||||
import { getOutputDirectory } from '../../prerender/utils.js';
|
||||
import type { SSRManifestI18n } from '../app/types.js';
|
||||
|
|
|
@ -8,6 +8,7 @@ import type {
|
|||
import { getOutputDirectory } from '../../prerender/utils.js';
|
||||
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';
|
||||
import type { SSRManifest } from '../app/types.js';
|
||||
import type { TryRewriteResult } from '../base-pipeline.js';
|
||||
import { routeIsFallback, routeIsRedirect } from '../redirects/helpers.js';
|
||||
import { RedirectSinglePageBuiltModule } from '../redirects/index.js';
|
||||
import { Pipeline } from '../render/index.js';
|
||||
|
@ -26,7 +27,6 @@ import { RESOLVED_SPLIT_MODULE_ID } from './plugins/plugin-ssr.js';
|
|||
import { getPagesFromVirtualModulePageName, getVirtualModulePageName } from './plugins/util.js';
|
||||
import type { PageBuildData, SinglePageBuiltModule, StaticBuildOptions } from './types.js';
|
||||
import { i18nHasFallback } from './util.js';
|
||||
import type {TryRewriteResult} from "../base-pipeline.js";
|
||||
|
||||
/**
|
||||
* The build pipeline is responsible to gather the files emitted by the SSR build and generate the pages by executing these files.
|
||||
|
@ -291,7 +291,7 @@ export class BuildPipeline extends Pipeline {
|
|||
request: Request,
|
||||
_sourceRoute: RouteData,
|
||||
): Promise<TryRewriteResult> {
|
||||
const { routeData, pathname, newUrl} = findRouteToRewrite({
|
||||
const { routeData, pathname, newUrl } = findRouteToRewrite({
|
||||
payload,
|
||||
request,
|
||||
routes: this.options.manifest.routes,
|
||||
|
|
|
@ -4,7 +4,7 @@ import type { OutputChunk } from 'rollup';
|
|||
import type { Plugin as VitePlugin } from 'vite';
|
||||
import { getAssetsPrefix } from '../../../assets/utils/getAssetsPrefix.js';
|
||||
import { normalizeTheLocale } from '../../../i18n/index.js';
|
||||
import {toFallbackType, toRoutingStrategy} from '../../../i18n/utils.js';
|
||||
import { toFallbackType, toRoutingStrategy } from '../../../i18n/utils.js';
|
||||
import { runHookBuildSsr } from '../../../integrations/hooks.js';
|
||||
import { BEFORE_HYDRATION_SCRIPT_ID, PAGE_SCRIPT_ID } from '../../../vite-plugin-scripts/index.js';
|
||||
import type {
|
||||
|
|
|
@ -403,7 +403,7 @@ export const AstroConfigSchema = z.object({
|
|||
.object({
|
||||
prefixDefaultLocale: z.boolean().optional().default(false),
|
||||
redirectToDefaultLocale: z.boolean().optional().default(true),
|
||||
fallbackType: z.enum(["redirect", "rewrite"]).optional().default("redirect"),
|
||||
fallbackType: z.enum(['redirect', 'rewrite']).optional().default('redirect'),
|
||||
})
|
||||
.refine(
|
||||
({ prefixDefaultLocale, redirectToDefaultLocale }) => {
|
||||
|
|
|
@ -142,7 +142,7 @@ export class RenderContext {
|
|||
if (payload) {
|
||||
pipeline.logger.debug('router', 'Called rewriting to:', payload);
|
||||
// we intentionally let the error bubble up
|
||||
const { routeData, componentInstance: newComponent} = await pipeline.tryRewrite(
|
||||
const { routeData, componentInstance: newComponent } = await pipeline.tryRewrite(
|
||||
payload,
|
||||
this.request,
|
||||
this.originalRoute,
|
||||
|
|
|
@ -58,15 +58,15 @@ export function findRouteToRewrite({
|
|||
if (foundRoute) {
|
||||
return {
|
||||
routeData: foundRoute,
|
||||
newUrl,
|
||||
pathname
|
||||
newUrl,
|
||||
pathname,
|
||||
};
|
||||
} else {
|
||||
const custom404 = routes.find((route) => route.route === '/404');
|
||||
if (custom404) {
|
||||
return { routeData: custom404, newUrl, pathname };
|
||||
} else {
|
||||
return { routeData: DEFAULT_404_ROUTE, newUrl, pathname };
|
||||
return { routeData: DEFAULT_404_ROUTE, newUrl, pathname };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -282,7 +282,7 @@ export type MiddlewarePayload = {
|
|||
defaultLocale: string;
|
||||
domains: Record<string, string> | undefined;
|
||||
fallback: Record<string, string> | undefined;
|
||||
fallbackType: "redirect" | "rewrite";
|
||||
fallbackType: 'redirect' | 'rewrite';
|
||||
};
|
||||
|
||||
// NOTE: public function exported to the users via `astro:i18n` module
|
||||
|
@ -341,7 +341,7 @@ export function redirectToFallback({
|
|||
defaultLocale,
|
||||
strategy,
|
||||
base,
|
||||
fallbackType
|
||||
fallbackType,
|
||||
}: MiddlewarePayload) {
|
||||
return async function (context: APIContext, response: Response): Promise<Response> {
|
||||
if (response.status >= 300 && fallback) {
|
||||
|
@ -378,7 +378,7 @@ export function redirectToFallback({
|
|||
newPathname = context.url.pathname.replace(`/${urlLocale}`, `/${pathFallbackLocale}`);
|
||||
}
|
||||
|
||||
if (fallbackType === "rewrite") {
|
||||
if (fallbackType === 'rewrite') {
|
||||
return await context.rewrite(newPathname);
|
||||
} else {
|
||||
return context.redirect(newPathname);
|
||||
|
|
|
@ -216,10 +216,11 @@ export function toRoutingStrategy(
|
|||
return strategy;
|
||||
}
|
||||
|
||||
export function toFallbackType(routing: NonNullable<AstroConfig['i18n']>['routing']): "redirect" | "rewrite" {
|
||||
export function toFallbackType(
|
||||
routing: NonNullable<AstroConfig['i18n']>['routing'],
|
||||
): 'redirect' | 'rewrite' {
|
||||
if (routing === 'manual') {
|
||||
return 'rewrite';
|
||||
}
|
||||
return routing.fallbackType;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import { IncorrectStrategyForI18n } from '../core/errors/errors-data.js';
|
|||
import { AstroError } from '../core/errors/index.js';
|
||||
import * as I18nInternals from '../i18n/index.js';
|
||||
import type { RedirectToFallback } from '../i18n/index.js';
|
||||
import {toFallbackType, toRoutingStrategy} from '../i18n/utils.js';
|
||||
import { toFallbackType, toRoutingStrategy } from '../i18n/utils.js';
|
||||
import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js';
|
||||
|
||||
export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js';
|
||||
|
@ -268,7 +268,7 @@ if (i18n?.routing === 'manual') {
|
|||
strategy,
|
||||
domains,
|
||||
fallback,
|
||||
fallbackType
|
||||
fallbackType,
|
||||
});
|
||||
} else {
|
||||
redirectToDefaultLocale = noop('redirectToDefaultLocale');
|
||||
|
@ -297,7 +297,7 @@ if (i18n?.routing === 'manual') {
|
|||
strategy,
|
||||
domains,
|
||||
fallback,
|
||||
fallbackType
|
||||
fallbackType,
|
||||
});
|
||||
} else {
|
||||
notFound = noop('notFound');
|
||||
|
@ -334,7 +334,7 @@ if (i18n?.routing === 'manual') {
|
|||
strategy,
|
||||
domains,
|
||||
fallback,
|
||||
fallbackType
|
||||
fallbackType,
|
||||
});
|
||||
} else {
|
||||
redirectToFallback = noop('useFallback');
|
||||
|
@ -384,7 +384,7 @@ if (i18n?.routing === 'manual') {
|
|||
fallback: undefined,
|
||||
strategy,
|
||||
domainLookupTable: {},
|
||||
fallbackType
|
||||
fallbackType,
|
||||
};
|
||||
return I18nInternals.createMiddleware(manifest, base, trailingSlash, format);
|
||||
};
|
||||
|
|
|
@ -12,7 +12,7 @@ import type { Logger } from '../core/logger/core.js';
|
|||
import { createViteLoader } from '../core/module-loader/index.js';
|
||||
import { injectDefaultRoutes } from '../core/routing/default.js';
|
||||
import { createRouteManifest } from '../core/routing/index.js';
|
||||
import {toFallbackType, toRoutingStrategy} from '../i18n/utils.js';
|
||||
import { toFallbackType, toRoutingStrategy } from '../i18n/utils.js';
|
||||
import { baseMiddleware } from './base.js';
|
||||
import { createController } from './controller.js';
|
||||
import { recordServerError } from './error.js';
|
||||
|
|
|
@ -1930,8 +1930,6 @@ describe('SSR fallback from missing locale index to default locale index', () =>
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
describe('Fallback rewrite dev server', () => {
|
||||
/** @type {import('./test-utils').Fixture} */
|
||||
let fixture;
|
||||
|
@ -1949,14 +1947,14 @@ describe('Fallback rewrite dev server', () => {
|
|||
fallback: {
|
||||
fr: 'en',
|
||||
},
|
||||
fallbackType: "rewrite"
|
||||
fallbackType: 'rewrite',
|
||||
},
|
||||
});
|
||||
devServer = await fixture.startDevServer();
|
||||
});
|
||||
after(async () => {
|
||||
devServer.stop()
|
||||
})
|
||||
devServer.stop();
|
||||
});
|
||||
|
||||
it('should correctly rewrite to en', async () => {
|
||||
const html = await fixture.fetch('/fr').then((res) => res.text());
|
||||
|
@ -1977,7 +1975,7 @@ describe('Fallback rewrite SSG', () => {
|
|||
locales: ['en', 'fr'],
|
||||
routing: {
|
||||
prefixDefaultLocale: false,
|
||||
fallbackType: "rewrite"
|
||||
fallbackType: 'rewrite',
|
||||
},
|
||||
fallback: {
|
||||
fr: 'en',
|
||||
|
@ -2015,7 +2013,7 @@ describe('Fallback rewrite SSR', () => {
|
|||
locales: ['en', 'fr'],
|
||||
routing: {
|
||||
prefixDefaultLocale: false,
|
||||
fallbackType: "rewrite"
|
||||
fallbackType: 'rewrite',
|
||||
},
|
||||
fallback: {
|
||||
fr: 'en',
|
||||
|
|
|
@ -436,7 +436,7 @@ export function getProductionCurrentSnapshot(options: {
|
|||
|
||||
async function getDbCurrentSnapshot(
|
||||
appToken: string,
|
||||
remoteUrl: string
|
||||
remoteUrl: string,
|
||||
): Promise<DBSnapshot | undefined> {
|
||||
const client = createRemoteDatabaseClient({
|
||||
dbType: 'libsql',
|
||||
|
@ -447,7 +447,7 @@ async function getDbCurrentSnapshot(
|
|||
try {
|
||||
const res = await client.get<{ snapshot: string }>(
|
||||
// Latest snapshot
|
||||
sql`select snapshot from _astro_db_snapshot order by id desc limit 1;`
|
||||
sql`select snapshot from _astro_db_snapshot order by id desc limit 1;`,
|
||||
);
|
||||
|
||||
return JSON.parse(res.snapshot);
|
||||
|
@ -464,7 +464,7 @@ async function getDbCurrentSnapshot(
|
|||
|
||||
async function getStudioCurrentSnapshot(
|
||||
appToken: string,
|
||||
remoteUrl: string
|
||||
remoteUrl: string,
|
||||
): Promise<DBSnapshot | undefined> {
|
||||
const url = new URL('/db/schema', remoteUrl);
|
||||
|
||||
|
|
|
@ -9,7 +9,12 @@ import { DB_PATH, RUNTIME_IMPORT, RUNTIME_VIRTUAL_IMPORT, VIRTUAL_MODULE_ID } fr
|
|||
import { getResolvedFileUrl } from '../load-file.js';
|
||||
import { SEED_DEV_FILE_NAME, getCreateIndexQueries, getCreateTableQuery } from '../queries.js';
|
||||
import type { DBTables } from '../types.js';
|
||||
import { type VitePlugin, getAstroEnv, getDbDirectoryUrl, getRemoteDatabaseInfo } from '../utils.js';
|
||||
import {
|
||||
type VitePlugin,
|
||||
getAstroEnv,
|
||||
getDbDirectoryUrl,
|
||||
getRemoteDatabaseInfo,
|
||||
} from '../utils.js';
|
||||
|
||||
export const resolved = {
|
||||
module: '\0' + VIRTUAL_MODULE_ID,
|
||||
|
|
|
@ -19,15 +19,17 @@ export function getRemoteDatabaseInfo(): RemoteDatabaseInfo {
|
|||
const astroEnv = getAstroEnv();
|
||||
const studioEnv = getAstroStudioEnv();
|
||||
|
||||
if (studioEnv.ASTRO_STUDIO_REMOTE_DB_URL) return {
|
||||
type: 'studio',
|
||||
url: studioEnv.ASTRO_STUDIO_REMOTE_DB_URL,
|
||||
};
|
||||
if (studioEnv.ASTRO_STUDIO_REMOTE_DB_URL)
|
||||
return {
|
||||
type: 'studio',
|
||||
url: studioEnv.ASTRO_STUDIO_REMOTE_DB_URL,
|
||||
};
|
||||
|
||||
if (astroEnv.ASTRO_DB_REMOTE_URL) return {
|
||||
type: 'libsql',
|
||||
url: astroEnv.ASTRO_DB_REMOTE_URL,
|
||||
};
|
||||
if (astroEnv.ASTRO_DB_REMOTE_URL)
|
||||
return {
|
||||
type: 'libsql',
|
||||
url: astroEnv.ASTRO_DB_REMOTE_URL,
|
||||
};
|
||||
|
||||
return {
|
||||
type: 'studio',
|
||||
|
|
|
@ -43,10 +43,10 @@ const remoteResultSchema = z.object({
|
|||
});
|
||||
|
||||
type RemoteDbClientOptions = {
|
||||
dbType: 'studio' | 'libsql',
|
||||
appToken: string,
|
||||
remoteUrl: string | URL,
|
||||
}
|
||||
dbType: 'studio' | 'libsql';
|
||||
appToken: string;
|
||||
remoteUrl: string | URL;
|
||||
};
|
||||
|
||||
export function createRemoteDatabaseClient(options: RemoteDbClientOptions) {
|
||||
const remoteUrl = new URL(options.remoteUrl);
|
||||
|
@ -65,7 +65,7 @@ function createRemoteLibSQLClient(appToken: string, remoteDbURL: URL) {
|
|||
authToken: appToken,
|
||||
url: remoteDbURL.protocol === 'memory:' ? ':memory:' : remoteDbURL.toString(),
|
||||
});
|
||||
return drizzleLibsql(client);
|
||||
return drizzleLibsql(client);
|
||||
}
|
||||
|
||||
function createStudioDatabaseClient(appToken: string, remoteDbURL: URL) {
|
||||
|
|
Loading…
Add table
Reference in a new issue