Home / File/ serve-static.ts — astro Source File

serve-static.ts — astro Source File

Architecture documentation for serve-static.ts, a typescript file in the astro codebase. 8 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 8 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  6f42a928_6dfc_6650_ab39_c8d254e11681["serve-static.ts"]
  b384cf30_390d_6db6_4724_be96d46e50ef["./types.js"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> b384cf30_390d_6db6_4724_be96d46e50ef
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  c2f6615e_96e9_c4eb_5f71_cf120e271705["node:http"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> c2f6615e_96e9_c4eb_5f71_cf120e271705
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  e4df8f29_fb2f_3d70_a962_fdf6a3670b22["path"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> e4df8f29_fb2f_3d70_a962_fdf6a3670b22
  8d666858_ab9a_2697_4536_5cb6ce038643["node"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> 8d666858_ab9a_2697_4536_5cb6ce038643
  029c1e2b_8d2c_466f_0967_b1cb0851af98["send"]
  6f42a928_6dfc_6650_ab39_c8d254e11681 --> 029c1e2b_8d2c_466f_0967_b1cb0851af98
  style 6f42a928_6dfc_6650_ab39_c8d254e11681 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs';
import type { IncomingMessage, ServerResponse } from 'node:http';
import path from 'node:path';
import url from 'node:url';
import { hasFileExtension, isInternalPath } from '@astrojs/internal-helpers/path';
import type { NodeApp } from 'astro/app/node';
import send from 'send';
import type { Options } from './types.js';

/**
 * Creates a Node.js http listener for static files and prerendered pages.
 * In standalone mode, the static handler is queried first for the static files.
 * If one matching the request path is not found, it relegates to the SSR handler.
 * Intended to be used only in the standalone mode.
 */
export function createStaticHandler(app: NodeApp, options: Options) {
	const client = resolveClientDir(options);
	/**
	 * @param ssr The SSR handler to be called if the static handler does not find a matching file.
	 */
	return (req: IncomingMessage, res: ServerResponse, ssr: () => unknown) => {
		if (req.url) {
			// There might be cases where the incoming URL has the #, which we want to remove.
			let fullUrl = req.url;
			if (req.url.includes('#')) {
				fullUrl = fullUrl.slice(0, req.url.indexOf('#'));
			}

			const [urlPath, urlQuery] = fullUrl.split('?');
			const filePath = path.join(client, app.removeBase(urlPath));

			let isDirectory = false;
			try {
				isDirectory = fs.lstatSync(filePath).isDirectory();
			} catch {}

			const { trailingSlash = 'ignore' } = options;

			const hasSlash = urlPath.endsWith('/');
			let pathname = urlPath;

			if (app.headersMap && app.headersMap.length > 0) {
				const routeData = app.match(req, true);
				if (routeData && routeData.prerender) {
					const matchedRoute = app.headersMap.find((header) => header.pathname.includes(pathname));
					if (matchedRoute) {
						for (const header of matchedRoute.headers) {
							res.setHeader(header.key, header.value);
						}
					}
				}
			}

			switch (trailingSlash) {
				case 'never': {
					if (isDirectory && urlPath !== '/' && hasSlash) {
						pathname = urlPath.slice(0, -1) + (urlQuery ? '?' + urlQuery : '');
						res.statusCode = 301;
						res.setHeader('Location', pathname);
						return res.end();
// ... (89 more lines)

Domain

Subdomains

Dependencies

  • ./types.js
  • node
  • node:fs
  • node:http
  • node:path
  • node:url
  • path
  • send

Frequently Asked Questions

What does serve-static.ts do?
serve-static.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in serve-static.ts?
serve-static.ts defines 4 function(s): appendForwardSlash, createStaticHandler, prependForwardSlash, resolveClientDir.
What does serve-static.ts depend on?
serve-static.ts imports 8 module(s): ./types.js, node, node:fs, node:http, node:path, node:url, path, send.
Where is serve-static.ts in the architecture?
serve-static.ts is located at packages/integrations/node/src/serve-static.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/integrations/node/src).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free