Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SSR create function async #17235

Closed
yusufkandemir opened this issue May 27, 2024 · 3 comments
Closed

Make SSR create function async #17235

yusufkandemir opened this issue May 27, 2024 · 3 comments

Comments

@yusufkandemir
Copy link
Member

In order to use Fastify with SSR, you would need to use a compatibility layer such as @fastify/middie or @fastify/express. That is because Quasar uses and expects connect/express-like middleware. However, the registration process for those plugins is async, which is problematic as create is expected to be synchronous.
https://github.com/fastify/middie#usage

For the dev web server, we should be able to just put an await here:

const app = middlewareParams.app = create(middlewareParams)

For the production web server, we should be able to move the create call and app interaction logic into isReady which is async.

const app = create(middlewareParams)
// fill in "app" for next calls
middlewareParams.app = app
<% if (ssr.pwa) { %>
// serve the service worker with no cache
app.use(resolveUrlPath('/<%= pwa.swFilename %>'), serveStatic('<%= pwa.swFilename %>', { maxAge: 0 }))
<% } %>
// serve "client" folder (includes the "public" folder)
app.use(resolveUrlPath('/'), serveStatic('.'))
const isReady = () => injectMiddlewares(middlewareParams)

@FadhiliNjagi
Copy link

FadhiliNjagi commented Jul 6, 2024

I second this.

In the docs, it says that you can use anything you want for your backend server, yet in practice there are obstacles to doing this:

  1. The create function in src-ssr/server.ts is sync only with a warning that is should not be async. So replacing Express with @fastify/express or @fastify/middie Fastify plugins does not work (they are registered asynchronously).
  2. Express-specific middleware are injected in the dev server in between create and listen which breaks Fastify apps. If more control was given over this, for example application devs having to call these middlewares ourselves, then we could register @fastify/middie plugin in listen function then "use" all those middlewares.
  3. In middleware files generated by the cli (quasar new ssrmiddleware myMw), the type of app is hardcoded as Express. So to use Fastify or anything other than express is a pain since you have to lie to the TS compiler by casting to unknown then to Fastify App. Possible solution: make the type of app as ReturnType<typeof create>.
  4. The serveStaticContent function in src-ssr/server.ts is expected to be an Express middleware. Using another backend framework only to be locked in to serving static files using express kind of beats the whole point.

Quasar SSR seems too opinionated towards Express at the moment. It's just too much hustle to try and fight it in order to use Fastify. I might try Nuxt with Fastify on future SSR projects.

@rstoenescu
Copy link
Member

rstoenescu commented Jul 29, 2024

The "create" function can be async in q/app-vite 2.0.0-beta.16 (unreleased yet).
But I'm not done yet. Will push more changes to ease the SSR usage.

@rstoenescu
Copy link
Member

Improvements will be available in q/app-vite 2.0.0-beta.16

rstoenescu added a commit that referenced this issue Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants