Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

♡ Smart placeholder for missing assets

License

NotificationsYou must be signed in to change notification settings

unjs/serve-placeholder

Repository files navigation

npm versionnpm downloads

Smart placeholder for missing assets

Why?

💵 Rendering Errors is costly

Serving each 404 page for assets adds extra load to the server and increases crashing chances. This is crucial for setups with server-side-rendering and removes additional SSR loads when assets likerobots.txt orfavicon.ico don't exist.

👌 Meaningful Responses

We can always send a better 404 response than an HTML page by knowing file extensions. For example, we send a fallback transparent 1x1 image for image extensions.

🔍 SEO Friendly

Instead of indexing invalid URLs with HTML pages, we properly send 404 and the right content type.

Usage

Install package:

# ✨ Auto-detectnpx nypm install serve-placeholder# npmnpm install serve-placeholder# yarnyarn add serve-placeholder# pnpmpnpm install serve-placeholder# bunbun install serve-placeholder

Import:

// ESMimport{servePlaceholder}from"serve-placeholder";// CommonJSconst{ servePlaceholder}=require("serve-placeholder");

Create and add server middleware between serve-static and router middleware:

app.use('/assets', serveStatic(..))++ app.use('/assets', servePlaceholder())app.use('/', router)

Additionally, we can have a default placeholder for arbitrary routes which handles known extensionsassuming other routes have no extension:

app.use('/assets', serveStatic(..))app.use('/assets', servePlaceholder())++ app.use('/', placeholder({ skipUnknown: true }))app.use('/', router)

Options

handlers

A mapping from file extensions to the handler. Extensions should start withdot like.js.

You can disable any of the handlers by setting the value tonull

If the value of a handler is set tofalse, the middleware will be ignored for that extension.

statusCode

  • Default:404

SetsstatusCode for all handled responses. Set tofalse to disable overriding statusCode.

skipUnknown

  • Default:false

Skip middleware when no handler is defined for the current request.

Please note that if this option is set totrue, thendefault handler will be disabled!

placeholders

  • Type:Object

A mapping from handler to placeholder. Values can beString orBuffer. You can disable any of the placeholders by setting the value tofalse.

mimes

  • Type:Object

A mapping from handler to the mime type. Mime type will be set asContent-Type header. You can disable sending any of the mimes by setting the value tofalse.

cacheHeaders

  • Default:true

Set headers to prevent accidentally caching 404 resources.

When enabled, these headers will be sent:

constheaders={"cache-control":"no-cache, no-store, must-revalidate",expires:"0",pragma:"no-cache",};

placeholderHeader

  • Default:true

Sets anX-Placeholder header with value of handler name.

Defaults

These aredefault handlers. You can override every of them using provided options.

HandlerExtensionsMime typePlaceholder
defaultany unknown extension--
css.csstext/css/* style not found */
html.html,.htmtext/html<!-- page not found -->
js.jsapplication/javascript/* script not found */
json.jsonapplication/json{}
map.mapapplication/json[empty sourcemap v3 json]
plain.txt,.text,.mdtext/plain[empty]
image.png,.jpg,.jpeg,.gif,.svg,.webp,.bmp,.icoimage/gif[transparent 1x1 image]

Development

local development
  • Clone this repository
  • Install latest LTS version ofNode.js
  • EnableCorepack usingcorepack enable
  • Install dependencies usingpnpm install
  • Run interactive tests usingpnpm dev

License

Published under theMIT license.Made by@pi0 andcommunity 💛


🤖 auto updated withautomd


[8]ページ先頭

©2009-2025 Movatter.jp