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

[legacy-framework] Add support for async experimental.initServer() #2798

Merged
merged 2 commits into from
Oct 20, 2021

Conversation

g3offrey
Copy link

@g3offrey g3offrey commented Oct 4, 2021

Closes: blitz-js/legacy-framework#279

What are the changes and their implications?

I did the solution proposed by @flybayer on the issue blitz-js/legacy-framework#279

To do so I had to load the config before the server is listening. Indeed today, the config loading is done at first on prepare method, which is run on the server listening.

The loadConfig method was private so I had to set it a public 😢
Another drawback of this is that the loadConfig is now done two times: before the listening and when we call getServer (at the listening).

I was thinking about two things doing that, moving the initServer logic inside a NextServer method (onServerInit ?), so I can set back loadConfig as private.

Or maybe instead of calling loadConfig, I can call getServer (setting it public before so) which has a loadConfig inside of it. This has advantages of preparing the serverPromise per advance and won't do a duplicate call to loadConfig.

What do you think ? 🤔

(As this is an experimental feature at the moment, we can log a refactoring ticket and do the changes on the future.)

Bug Checklist

  • Integration test added (see test docs if needed)

Feature Checklist

@flybayer
Copy link
Member

flybayer commented Oct 6, 2021

@g3offrey thank you so much!

Upon review, I think we could run initServer just before this line in getServer. What do you think?

@g3offrey
Copy link
Author

g3offrey commented Oct 6, 2021

@flybayer Don't worry, I'm glad to contribute to such a project 😄
Sure, I think this is possible, however I have to do some modification to do so.

First, at the moment the getServer method is called first on prepare, so after listening. (This is not what we want)

With my modification, we can call it once in getRequestHandler instead of at every request (in request handler).
Also this method is called before the listening so it is perfect for our use-case.

 async getRequestHandler() {
    const requestHandler = await this.getServerRequestHandler()

    return (
      req: IncomingMessage,
      res: ServerResponse,
      parsedUrl?: UrlWithParsedQuery
    ) => {
      return requestHandler(req, res, parsedUrl)
    }
  }

As now getRequestHandler is simply a wrapper to getServerRequestHandler. Maybe in the future we can delete this method and call directly getServerRequestHandler or move content from one to another. I had to change all the calls as now this method is async.

With these modifications, I can implement your solution. What do you think? Do you prefer this way of doing things or the previous one? 😄

Check out the last commit. I hope I am clear enough, that's not easy by text 😅

@g3offrey g3offrey force-pushed the feat/add-async-initserver branch 4 times, most recently from e9f3bd3 to d24db08 Compare October 6, 2021 23:06
@flybayer
Copy link
Member

Hey @g3offrey so sorry for then long delay here. I really appreciate your work on this. I spent some time analyzing this, and I think we were following a constraint that we don't need. I can't see any reason that we have to await initServer before the http server is listening. Because initServer can't change the listening port or anything.

So I think we can just add await initServer in getServer without all the other changes. What do you think?

And in general, we want to have a solution that minimizes changes since we are a fork of nextjs and merge in the their changes.

@g3offrey g3offrey force-pushed the feat/add-async-initserver branch from d24db08 to 40c5503 Compare October 17, 2021 11:31
@g3offrey
Copy link
Author

Hi @flybayer

Don't worry, it seems to be a big piece of changes, so I totally understand that it is long to review.
You know more information than me on Blitz codebase, so I am totally fine with your solution.

I think I implemented it on my latest commit (after reverting previous code). Please check it out and tell me if any changes are required 😄

@flybayer
Copy link
Member

Awesome, thank you so much!!

@flybayer flybayer changed the title enable user to do async initServer Add support for async experimental.initServer() Oct 20, 2021
@flybayer flybayer merged commit 8fc8800 into blitz-js:canary Oct 20, 2021
@itsdillon itsdillon changed the title Add support for async experimental.initServer() [legacy-framework] Add support for async experimental.initServer() Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support async initServer hook
3 participants