Repository: withastro/astro
File: packages/astro/src/core/build/generate.ts (TypeScript)

Code before:
	pipeline: BuildPipeline,
	gopts: GeneratePathOptions,
	route: RouteData,
): Promise<boolean | undefined> {
	const { mod } = gopts;
	const { config, logger, options } = pipeline;
	logger.debug('build', `Generating: ${pathname}`);

	// This adds the page name to the array so it can be shown as part of stats.
	if (route.type === 'page') {
		addPageName(pathname, options);
	}

	// Do not render the fallback route if there is already a translated page
	// with the same path
	if (
		route.type === 'fallback' &&
		// If route is index page, continue rendering. The index page should
		// always be rendered
		route.pathname !== '/' &&
		// Check if there is a translated page with the same path
		Object.values(options.allPages).some((val) => {
			if (val.route.pattern.test(pathname)) {
				// Check if we've matched a dynamic route
				if (val.route.segments && val.route.segments.length !== 0) {
					let locale = removeLeadingForwardSlash(pathname).split('/')[0];
					// Check that the route is in a matching locale folder
					if (val.route.segments[0][0].content !== locale) return false;
				}
				// Route matches
				return true;
			} else {
				// Pattern doesn't match
				return false;
			}
		})
	) {
		return void 0;
	}

	const url = getUrlForPath(
		pathname,
		config.base,
		options.origin,
		config.build.format,
		config.trailingSlash,
		route.type,
	);

	const request = createRequest({
		url,

Review comment (refactor):
I think we can move this outside of the loop, there's no need to create this variable at each iteration. Also, the project can have `/base` set, which means that the locale is **after** base e.g. `/base/es`.

You should probably add a test case with `base` too.

Code after:
}

/**
 *
 * @param pathname
 * @param pipeline
 * @param gopts
 * @param route
 * @return {Promise<boolean | undefined>} If `false` the file hasn't been created. If `undefined` it's expected to not be created.
 */
async function generatePath(
	pathname: string,
	pipeline: BuildPipeline,
	gopts: GeneratePathOptions,
	route: RouteData,
): Promise<boolean | undefined> {
	const { mod } = gopts;
	const { config, logger, options } = pipeline;
	logger.debug('build', `Generating: ${pathname}`);

	// This adds the page name to the array so it can be shown as part of stats.
	if (route.type === 'page') {
		addPageName(pathname, options);
	}

	// Do not render the fallback route if there is already a translated page
	// with the same path
	if (route.type === 'fallback' && route.pathname !== '/') {
		// Get the locale from the pathname
		let locale = removeLeadingForwardSlash(pathname).split('/')[0];
		if (
			Object.values(options.allPages).some((val) => {
				if (val.route.pattern.test(pathname)) {
					// Check if we've matched a dynamic route
					if (val.route.segments && val.route.segments.length !== 0) {
						// Check that the route is in a matching locale folder
						if (val.route.segments[0][0].content !== locale) return false;
					}
					// Route matches
					return true;
				} else {
					return false;
				}
			})
		) {
			return undefined;
		}
	}

	const url = getUrlForPath(
		pathname,