Giter VIP home page Giter VIP logo

serve-placeholder's Introduction

♡ serve-placeholder

Smart placeholder for missing assets

npm version npm downloads Github Actions Codecov

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 like robots.txt or favicon.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:

# npm
npm install serve-placeholder

# yarn
yarn install serve-placeholder

# pnpm
pnpm install serve-placeholder

Import:

// ESM
import { servePlaceholder } from 'serve-placeholder'

// CommonJS
const { 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 extensions assuming 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 with dot like .js.

You can disable any of the handlers by setting the value to null

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

statusCode

  • Default: 404

Sets statusCode for all handled responses. Set to false 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 to true, then default handler will be disabled!

placeholders

  • Type: Object

A mapping from handler to placeholder. Values can be String or Buffer. You can disable any of the placeholders by setting the value to false.

mimes

  • Type: Object

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

cacheHeaders

  • Default: true

Set headers to prevent accidentally caching 404 resources.

When enabled, these headers will be sent:

{
  'cache-control': 'no-cache, no-store, must-revalidate',
  'expires': '0',
  'pragma': 'no-cache'
}

placeholderHeader

  • Default: true

Sets an X-Placeholder header with value of handler name.

Defaults

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

Handler Extensions Mime type Placeholder
default any unknown extension - -
css .css text/css /* style not found */
html .html, .htm text/html <!-- page not found -->
js .js application/javascript /* script not found */
json .json application/json {}
map .map application/json [empty sourcemap v3 json]
plain .txt, .text, .md text/plain [empty]
image .png, .jpg, .jpeg, .gif, .svg, .webp, .bmp, .ico image/gif [transparent 1x1 image]

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Made with 💛

Published under MIT License.

serve-placeholder's People

Contributors

atinux avatar clarkdo avatar danielroe avatar luc122c avatar nozomuikuta avatar pi0 avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

serve-placeholder's Issues

serve-placeholder should not check for query param

Version

v2.4.3

Reproduction link

http://egame.qq.com/?ADTAG=g.map

Steps to reproduce

打开http://egame.qq.com/?ADTAG=g.map 返回404
打开http://egame.qq.com/?ADTAG=g 正确返回

serve-placeholder组件处理url时没有正确处理查询参数,导致查询参数命中规则

The serve-placeholder component does not correctly process the query parameters when processing the url, resulting in query parameter hit rules.

What is expected ?

正确返回

Return correctly

What is actually happening?

返回404

Return 404

This bug report is available on Nuxt community (#c8820)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-node v3
  • codecov/codecov-action v3
npm
package.json
  • defu ^6.0.0
  • c8 ^7.11.0
  • h3 ^0.7.2
  • listhen ^0.2.8
  • ohmyfetch ^0.4.15
  • vitest ^0.9.2
  • pnpm 6.32.4

  • Check this box to trigger a request for Renovate to run again on this repository

How to integrate with nuxt.js on koa

I am using nuxt.js programatically with the koa framework.
Should I call the placeholder middleware before or after calling the nuxt render method?

Allow setting cache headers

Or add some customisation point for setting response headers.

My CDN assumes responses w/o cache-control headers is cacheable and that breaks deploys since 404d bundles get stuck in cache during rollout.

Can we get updates in dependencies? Older version of defu is giving errors in IE11

I'm on node version 12.6.0
Getting error shown in the image everytime in IE11.

defu-error

package.json looks like this

"dependencies": {
"@nuxtjs/auth": "^4.8.4",
"@nuxtjs/axios": "^5.6.0",
"array-from-polyfill": "^1.0.1",
"axios": "^0.18.0",
"chart.js": "^2.8.0",
"chartjs-plugin-datalabels": "^0.6.0",
"cookieparser": "^0.1.0",
"cors": "^2.8.5",
"cross-env": "^5.2.0",
"crypto-js": "^3.1.9-1",
"csvtojson": "^2.0.8",
"dotenv": "^7.0.0",
"es6-promise": "^4.2.6",
"express": "^4.16.4",
"jquery": "^3.4.1",
"js-cookie": "^2.2.0",
"loadash": "^1.0.0",
"moment": "^2.24.0",
"moment-timezone": "^0.5.25",
"nuxt": "^2.5.1",
"nuxt-polyfill": "^1.0.3",
"qs": "^6.7.0",
"vue-mq": "^1.0.1",
"vue2-datepicker": "^2.8.0",
"vuejs-paginate2": "^2.2.1",
"vuetable-2": "^2.0.0-beta.4",
"vuex": "^3.1.0"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-vue": "^5.2.2",
"node-sass": "^4.12.0",
"nodemon": "^1.18.9",
"prettier": "^1.16.4",
"sass-loader": "^8.0.0"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.