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

POC: Avoid bundling middleware using react-server export conditions and vite plugin #10691

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented May 28, 2024

This is PoC (draft) - because we need to decide the direction we are going in with server routing.

Allows running OgGen middleware
This fixes running the OG Gen middleware partially i.e. it will build and run successfully, if I mock the App out like this:

- import App from './App'

export const registerMiddleware = async () => {
  const ogMw = new OgImageMiddleware({
- App,
+    App: ({ children }) => <>{children}</>,
    Document,
  })

  return [ogMw]
}

It achieves this by:

1. setting the react-server export condition in the middleware
This implies that all middleware, including auth middleware, will need to have these export conditions set inorder to work under the current RSC build system.

2. Adds a plugin to remove the registerMiddleware function from RSC Builds
(1) is not enough, Vite will still try to bundle

What doesn't work?
The OG Image middleware relies on us having access to App importing this in entry.server still causes build failures (due to React.context).

This could be resolved in one of two ways:

a) @Tobbe mention re-introducing App back into entry server (or some form of it)
If we have another change in the way server router works, this may be resolved, but I'm unsure how to proceed right now.

b) Maybe we need to fix all the imports from App from rwjs/auth, rwjs/web
This will allow us to import App under server conditions without blowing up.

@dac09 dac09 added the release:chore This PR is a chore (means nothing for users) label May 28, 2024
@dac09 dac09 added this to the RSC milestone May 28, 2024
@dac09 dac09 requested a review from Tobbe May 28, 2024 15:38
@@ -0,0 +1,9 @@
// This file is intentionally left empty, because we do not want to bundle middleware

export default function empty() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Temporary, so its easy to spot!

makeFilePath(rwPaths.web.distEntryServer)
)
}
entryServerImport = await import(makeFilePath(rwPaths.web.distEntryServer))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Middleware router still imports from the SSR builds, not the RSC ones (which doesnt include register middleware)

const plugin = () => ({
visitor: {
VariableDeclarator(path: NodePath<t.VariableDeclarator>) {
// @ts-expect-error fggg
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Need to cleanup if we go ahead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:chore This PR is a chore (means nothing for users)
Projects
Status: Backlog
Development

Successfully merging this pull request may close these issues.

None yet

1 participant