{"id":25420050,"url":"https://github.com/grommett/fastify-chunk-view","last_synced_at":"2026-05-18T03:08:46.055Z","repository":{"id":38452140,"uuid":"442593320","full_name":"grommett/fastify-chunk-view","owner":"grommett","description":"A Fastify plugin for sending HTML to the client as soon as it becomes available.","archived":false,"fork":false,"pushed_at":"2022-06-03T14:31:03.000Z","size":372,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-18T17:47:47.138Z","etag":null,"topics":["fastify","progressive-html","progressive-rendering","pssr"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grommett.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-28T21:56:50.000Z","updated_at":"2022-01-02T05:03:47.000Z","dependencies_parsed_at":"2022-08-20T02:21:41.791Z","dependency_job_id":null,"html_url":"https://github.com/grommett/fastify-chunk-view","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grommett%2Ffastify-chunk-view","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grommett%2Ffastify-chunk-view/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grommett%2Ffastify-chunk-view/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grommett%2Ffastify-chunk-view/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grommett","download_url":"https://codeload.github.com/grommett/fastify-chunk-view/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253850767,"owners_count":21973666,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","progressive-html","progressive-rendering","pssr"],"created_at":"2025-02-16T19:22:43.762Z","updated_at":"2025-10-11T20:06:53.021Z","avatar_url":"https://github.com/grommett.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chunk View\n\n\u003ca href=\"https://github.com/grommett/fastify-chunk-view/actions/workflows/ci.yaml\"\u003e\u003cimg src=\"https://github.com/grommett/fastify-chunk-view/workflows/CI/badge.svg\" /\u003e\u003c/a\u003e\n![Known Vulnerabilities](https://snyk.io/test/github/grommett/fastify-chunk-view/badge.svg)\n![Coverage](https://codecov.io/gh/grommett/fastify-chunk-view/branch/main/graph/badge.svg?token=SRZNNEZ5LD)\n\nA [Fastify](https://www.fastify.io/) plugin for sending HTML content to the client as soon as it becomes available.\n\n\n## Why\n\nThis plugin is ideal for situations where you have static content that can be served immediatley and async content that will arrive later. For example, a product list page where the header, styles and hero image can be sent right away while you grab the products from an API.\n\n## Install\n```js\nnpm i fastify-chunk-view --save\n```\n## Usage\n`fastify-chunk-view` supports ESM and CommonJS\n### ESM\n```js\nimport fastifyChunkView from 'fastify-chunk-view';\n```\n\n### Common JS\n```js\nconst fastifyChunkView = require('fastify-chunk-view');\n```\nThe `fastify-chunk-view` plugin takes an array of chunks and sends them to the client in that order. A chunk can be:\n- A `string`\n- A `function` that returns a `string`\n- An `async function` that returns a `string`\n- A `Readable` stream\n\nHave a look or run the `examples` folder in this repo for more details. Here's a silly example to illustate:\n\n```js\nfastify.get('/', (_req, reply) =\u003e {\n  reply.chunkView([\n    '\u003cp\u003etest chunk 1\u003c/p\u003e',\n    '\u003cp\u003etest chunk 2\u003c/p\u003e'\n  ]);\n});\n```\n\nIn the example above each chunk is pushed into the `reply`'s read stream in order.\n\nAn example of the ecommerce use case mentioned above:\n\n```js\nfastify.get('/', (_req, reply) =\u003e {\n  reply.chunkView([\n    // Sent to client immediately\n    '\u003chtml\u003e\u003chead\u003e\u003c/head\u003e\u003cbody\u003e\u003cimg src=\"a-big-marketing-image.jpg\"\u003e',\n    productList, // Sent after the list is retrieved\n    footer,\n    '\u003c/body\u003e\u003c/html\u003e'\n  ]);\n});\n\nasync function productList() {\n  const products = await getProducts();\n  return `\n      \u003cul\u003e\n        ${products.map(product =\u003e `\u003cli\u003e${product.name}\u003c/li\u003e`).join('')}\n      \u003c/ul\u003e`;\n}\n\nfunction getProducts() {\n  return new Promise(resolve =\u003e {\n    setTimeout(() =\u003e {\n      resolve([{ name: 'Product 1' }, { name: 'Product 2' }, { name: 'Product 3' }]);\n    }, 1000);\n  });\n}\n\nfunction footer() {\n  return `\u003cfooter\u003e${new Date().toLocaleDateString()}\u003c/footer\u003e`;\n}\n```\n\nYou probably wouldn't use the same strings over and over in your routes. This plugin also lets you use [`Readable`](https://nodejs.org/api/stream.html#readable-streams) streams.\n\nBuilding on the product list example above:\n\n```js\nfastify.get('/', (_req, reply) =\u003e {\n  const startHTML = fs.createReadStream('start.html');\n  const endHTML = fs.createReadStream('end.html');\n\n  reply.chunkView([\n    startHTML,\n    productList,\n    footer,\n    endHTML\n  ]);\n});\n\nasync function productList() {\n  const products = await getProducts();\n  return `\n      \u003cul\u003e\n        ${products.map(product =\u003e `\u003cli\u003e${product.name}\u003c/li\u003e`).join('')}\n      \u003c/ul\u003e`;\n}\n\nfunction footer() {\n  return `\u003cfooter\u003e${new Date().toLocaleDateString()}\u003c/footer\u003e`;\n}\n```\n\n## Further Reading\n- [The Lost Art of Progressive HTML Rendering](https://blog.codinghorror.com/the-lost-art-of-progressive-html-rendering/)\n- [Rediscovering Progressive HTML Rendering with Marko](https://tech.ebayinc.com/engineering/async-fragments-rediscovering-progressive-html-rendering-with-marko/)\n- [Progressive Rendering — The Key to Faster Web](https://medium.com/the-thinkmill/progressive-rendering-the-key-to-faster-web-ebfbbece41a4)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrommett%2Ffastify-chunk-view","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrommett%2Ffastify-chunk-view","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrommett%2Ffastify-chunk-view/lists"}