Home / Function/ createStaticHandler() — astro Function Reference

createStaticHandler() — astro Function Reference

Architecture documentation for the createStaticHandler() function in serve-static.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  5a2fbd03_4fb8_514e_1e0c_f298abe078e5["createStaticHandler()"]
  6f42a928_6dfc_6650_ab39_c8d254e11681["serve-static.ts"]
  5a2fbd03_4fb8_514e_1e0c_f298abe078e5 -->|defined in| 6f42a928_6dfc_6650_ab39_c8d254e11681
  6b657031_c1e0_9ceb_d4e4_6e9871dbb5c2["resolveClientDir()"]
  5a2fbd03_4fb8_514e_1e0c_f298abe078e5 -->|calls| 6b657031_c1e0_9ceb_d4e4_6e9871dbb5c2
  7a211938_0058_a4a6_f562_0a26ec132ff8["prependForwardSlash()"]
  5a2fbd03_4fb8_514e_1e0c_f298abe078e5 -->|calls| 7a211938_0058_a4a6_f562_0a26ec132ff8
  style 5a2fbd03_4fb8_514e_1e0c_f298abe078e5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/integrations/node/src/serve-static.ts lines 16–123

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();
					}
					if (isDirectory && !hasSlash) {
						pathname = `${urlPath}/index.html`;
					}
					break;
				}
				case 'ignore': {
					if (isDirectory && !hasSlash) {
						pathname = `${urlPath}/index.html`;
					}
					break;
				}
				case 'always': {
					// trailing slash is not added to "subresources"
					// We check if `urlPath` doesn't contain possible internal paths. This should prevent
					// redirects to unwanted paths
					if (!hasSlash && !hasFileExtension(urlPath) && !isInternalPath(urlPath)) {
						pathname = urlPath + '/' + (urlQuery ? '?' + urlQuery : '');
						res.statusCode = 301;
						res.setHeader('Location', pathname);
						return res.end();
					}
					break;
				}
			}
			// app.removeBase sometimes returns a path without a leading slash
			pathname = prependForwardSlash(app.removeBase(pathname));

			const stream = send(req, pathname, {
				root: client,
				dotfiles: pathname.startsWith('/.well-known/') ? 'allow' : 'deny',
			});

			let forwardError = false;

			stream.on('error', (err) => {

Domain

Subdomains

Frequently Asked Questions

What does createStaticHandler() do?
createStaticHandler() is a function in the astro codebase, defined in packages/integrations/node/src/serve-static.ts.
Where is createStaticHandler() defined?
createStaticHandler() is defined in packages/integrations/node/src/serve-static.ts at line 16.
What does createStaticHandler() call?
createStaticHandler() calls 2 function(s): prependForwardSlash, resolveClientDir.

Analyze Your Own Codebase

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

Try Supermodel Free