fastify / fastify-vite

Fastify plugin for Vite integration.
MIT License
849 stars 71 forks source link

Issues running with fastify-compress #115

Closed spaceemotion closed 9 months ago

spaceemotion commented 10 months ago

Prerequisites

Fastify version

4.24.1

Plugin version

4.0.0

Node.js version

20.2.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

docker

Description

After adding the compression plugin, i was only getting the following errors in console:

application-app-1             | [18:24:39 UTC] ERROR: premature close
application-app-1             |     reqId: "req-3"
application-app-1             |     err: {
application-app-1             |       "type": "Error",
application-app-1             |       "message": "premature close",
application-app-1             |       "stack":
application-app-1             |           Error: premature close
application-app-1             |               at onclosenexttick (/app/node_modules/.pnpm/end-of-stream@1.4.4/node_modules/end-of-stream/index.js:54:86)
application-app-1             |               at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
application-app-1             |     }

After some researching I found the following fix: https://github.com/fastify/fastify-compress/issues/215#issuecomment-1605282422

The vite plugin does not seem to return "this" from the html-callback. Overwriting the default config with the following (this is just for SPAs right now), works as it should:


await server.register(FastifyVite, {
  root: resolve(serverFolder, '../client'),
  spa: true,
  // Custom overwrite
  createHtmlFunction(source, scope, config) {
    const template = config.createHtmlTemplateFunction(source);

    return function () {
      this.type('text/html');
      this.send(template({ element: '' }));

      // fix compression issues
      return this;
    };
  },
});

Steps to Reproduce

  1. Install vite plugin to a fastify setup
  2. Install compression plugin
  3. Open the browser and try to load a page

Expected Behavior

No error upon load

galvez commented 10 months ago

Indeed, after Fastify v4 replies started having to be returned always in these cases.

This needs to be better reflected in the docs.

spaceemotion commented 10 months ago

So if I open a "simple" PR that only adds the return this; calls, would that do the trick? I can't really test the behavior outside my application right now. I don't know if this could break other plugins or how it would affect things in general.

Uzlopak commented 10 months ago

I actually wonder where we have to patch this, but yes. If you provide a PR we would consider it gracefully ;).

galvez commented 9 months ago

This has been fixed in the latest release.