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

Use universal-middleware #16

Merged
merged 36 commits into from
Nov 28, 2024
Merged

Use universal-middleware #16

merged 36 commits into from
Nov 28, 2024

Conversation

magne4000
Copy link
Member

@magne4000 magne4000 commented Oct 9, 2024

Notable changes

  • Use @universal-middleware/compress for compression
  • All cache related features have been dropped
  • getPageContext hook is removed. The whole context from universal middlewares is given as a Context to renderPage. This means that the same effect can be achieved by any custom universal middleware.
  • No more distinction between node and non-node envs (at least for now 🤞)

Next steps (not in this PR)

  • Put cache feature back?
  • Is it possible to have some generic way to handle HMR websocket connections, perhaps with crossws?

@jasonhilldm
Copy link

jasonhilldm commented Oct 14, 2024

Hi. I stumbled across this extension while trying to figure out the best way to deploy to Node and it looks awesome. The one issue I have is that I am using Auth.js for authentication and was previously using universal middleware to add the logged in user to the context.

When I use the vike-node extension though, my modified context is not being seen by Vike so I assume it's using a different context. I'm assuming these draft changes will mean my context changes will be seen by Vike via the use of universal middleware...is that right?

Not sure how far away these changes are, but is there another way that I can add the user to the context using regular middleware so that Vike will see that when I am using vike-node?

@magne4000
Copy link
Member Author

@jasonhilldm I would suggest to wait for this PR to be finished, as it will fix your issue.
Nevertheless, you could still retrieve the context for your server using the getContext function of your server inside the pageContext hook. Something like this should do the trick (untested, and probably does not work for all servers):

import { getContext } from "@universal-middleware/hono";

app.use(
  vike({
    pageContext: (req) => getContext(req)
  })
)

@jasonhilldm
Copy link

Thank you - that worked perfectly.

@magne4000 magne4000 force-pushed the magne4000/universal-middleware branch from 05aa8d6 to 775df55 Compare October 16, 2024 10:10
@magne4000 magne4000 requested a review from nitedani October 16, 2024 10:11
@magne4000 magne4000 marked this pull request as ready for review October 16, 2024 10:11
Copy link
Member

@brillout brillout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started reviewing a bit, I'll resume tomorrow.

packages/vike-node/README.md Outdated Show resolved Hide resolved
packages/vike-node/README.md Show resolved Hide resolved
packages/vike-node/README.md Outdated Show resolved Hide resolved
packages/vike-node/package.json Show resolved Hide resolved
packages/vike-node/src/vike.handler.ts Show resolved Hide resolved
packages/vike-node/src/runtime/vike-handler.ts Outdated Show resolved Hide resolved
packages/vike-node/src/runtime/vike-handler.ts Outdated Show resolved Hide resolved
packages/vike-node/src/runtime/vike-handler.ts Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
@magne4000
Copy link
Member Author

magne4000 commented Nov 25, 2024

Migration

  • Added to BREAKING CHANGE commits

0.1.x to 0.2.x

Caching support removed

app.use(
  vike({
    compress: false,
-     static: {
-       cache: false
-     }
  })
)

pageContext

If you were using it to feed universal-middleware context to pageContext, it's now the default behaviour.

Otherwise, you now need to create a universal context middleware and attach it to your server.

app.use(
  vike({
-    pageContext: (req) => ({
-      user: req.user
-    })
  })
)

@brillout
Copy link
Member

Add those as BREAKING CHANGES when squash merging

How about we push empty commits that include these BREAKING CHANGE: entries? It's a bit easier for the squasher.

@brillout
Copy link
Member

We can force push if you want to edit previous BREAKING CHANGE: entries.

@magne4000 magne4000 force-pushed the magne4000/universal-middleware branch 2 times, most recently from 90b5029 to 15a298e Compare November 25, 2024 14:31
@magne4000
Copy link
Member Author

BREAKING CHANGE commits updated

@brillout brillout mentioned this pull request Nov 25, 2024
4 tasks
packages/vike-node/package.json Show resolved Hide resolved
packages/vike-node/package.json Outdated Show resolved Hide resolved
packages/vike-node/package.json Outdated Show resolved Hide resolved
packages/vike-node/README.md Show resolved Hide resolved
@magne4000
Copy link
Member Author

Seems like we should be good to merge @brillout

@brillout
Copy link
Member

After bundling and publishing this middleware as @universal-middleware-examples/tool, one can then use this middleware as follows:

I think it's even more misleading. Many users will wonder "why should I publish my middleware?".

(I guess the docs is automatically copy-pasting real code from examples to the docs. FYI I used to do that as well but I stopped doing it precisely because of things like this.)

@brillout
Copy link
Member

A BREAKING CHANGE: is missing mentioning vike-node/connect.

Also:

- BREAKING CHANGE: `getPageContext` has been removed
+ BREAKING CHANGE: the `pageContext` setting has been removed

@brillout
Copy link
Member

LGTM otherwise. I've pushed a couple of commits, let me know if you disagree with any of it.

I've mixed feelings about removing the cache. I kinda liked that the compression was being cached, but I ain't sure whether caching should be done inside vike-node (nor compression for that matter). Maybe we could have vike-server-compress and vike-server-cache extensions. Also a vike-plus that automatically adds all kinds of nice-to-haves.

As for this PR, I'm a bit inclined to remove the compression outside of vike-node. But I'm fine if we keep it for now. We'll see how we want to structure everything as we move forward.

@magne4000 magne4000 force-pushed the magne4000/universal-middleware branch from f536a3f to 2be3c1b Compare November 27, 2024 17:36
@magne4000
Copy link
Member Author

LGTM otherwise. I've pushed a couple of commits, let me know if you disagree with any of it.

LGTM

I've mixed feelings about removing the cache. I kinda liked that the compression was being cached, but I ain't sure whether caching should be done inside vike-node (nor compression for that matter). Maybe we could have vike-server-compress and vike-server-cache extensions. Also a vike-plus that automatically adds all kinds of nice-to-haves.

As for this PR, I'm a bit inclined to remove the compression outside of vike-node. But I'm fine if we keep it for now. We'll see how we want to structure everything as we move forward.

I think that the caching layer do not belong directly in this package. I not even sure that I find the feature useful (have we measured that it really improves perfs?).

Regarding the compression, no strong feeling about it. For the sake of merging this PR sooner rather than later, I'd say we keep it as-is for now.

BREAKING CHANGE: cache related options have been removed

```diff
app.use(
  vike({
    compress: false,
-     static: {
-       cache: false
-     }
  })
)
```

# Conflicts:
#	packages/vike-node/src/runtime/vike-handler.ts
@magne4000 magne4000 force-pushed the magne4000/universal-middleware branch from 2be3c1b to 712809f Compare November 27, 2024 17:52
@brillout
Copy link
Member

A BREAKING CHANGE: is missing mentioning vike-node/connect.

Also:

- BREAKING CHANGE: `getPageContext` has been removed
+ BREAKING CHANGE: the `pageContext` setting has been removed

This seems to be still missing.

@brillout
Copy link
Member

I not even sure that I find the feature useful (have we measured that it really improves perfs?).

I see, good point. Indeed computing a cache key isn't gratis either and bloating memory isn't necessarily a good default.

Regarding the compression, no strong feeling about it. For the sake of merging this PR sooner rather than later, I'd say we keep it as-is for now.

Makes sense 👍

After bundling and publishing this middleware as @universal-middleware-examples/tool, one can then use this middleware as follows:

I think it's even more misleading. Many users will wonder "why should I publish my middleware?".

(I guess the docs is automatically copy-pasting real code from examples to the docs. FYI I used to do that as well but I stopped doing it precisely because of things like this.)

WDYT?

@magne4000
Copy link
Member Author

A BREAKING CHANGE: is missing mentioning vike-node/connect.
Also:

- BREAKING CHANGE: `getPageContext` has been removed
+ BREAKING CHANGE: the `pageContext` setting has been removed

This seems to be still missing.

Hum no, it's there 29128b5

@magne4000
Copy link
Member Author

After bundling and publishing this middleware as @universal-middleware-examples/tool, one can then use this middleware as follows:

I think it's even more misleading. Many users will wonder "why should I publish my middleware?".
(I guess the docs is automatically copy-pasting real code from examples to the docs. FYI I used to do that as well but I stopped doing it precisely because of things like this.)

WDYT?

I'm currently working on some universal-middleware doc, I'll see what I can come up with.

@brillout
Copy link
Member

brillout commented Nov 28, 2024

I meant this one:

A BREAKING CHANGE: is missing mentioning vike-node/connect.

BREAKING CHANGE: `vike-node/connect` replaced by `vike-node/express`
@magne4000
Copy link
Member Author

2a4be82

@brillout brillout merged commit a7554a7 into main Nov 28, 2024
4 checks passed
@brillout
Copy link
Member

Released as 0.2.0 🚀

@brillout
Copy link
Member

I made a couple of improvements to the readme, see the last commits. Let me know if you disagree with anything.

@magne4000
Copy link
Member Author

I made a couple of improvements to the readme, see the last commits. Let me know if you disagree with anything.

Thanks! LGTM

@brillout brillout deleted the magne4000/universal-middleware branch December 3, 2024 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants