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

Flagged SSR support #2548

Merged
merged 14 commits into from
Feb 14, 2022
Merged

Flagged SSR support #2548

merged 14 commits into from
Feb 14, 2022

Conversation

matthewp
Copy link
Contributor

@matthewp matthewp commented Feb 8, 2022

⚠️ If you are a user of Astro and see this PR and think that you can start deploying your app to a server and get SSR, slow down a second! This is only the initial flag and very basic support. Styles are not loading correctly at this point, for example. Like we did with the --experimental-static-build flag, this feature will be refined over the next few weeks/months and we'll let you know when its ready for community testing.

Changes

  • This adds a new --experimental-ssr flag to astro build which will result in dist/server/ and dist/client/ directories.
  • SSR can be used through this API:
    import { createServer } from 'http';
    import { loadApp } from 'astro/app/node';
    
    const app = await loadApp(new URL('./dist/server/', import.meta.url));
    
    createServer((req, res) => {
      let url = new URL(`http:https://${req.headers.host}${req.url}`);
      const route = app.match(url);
      if(route) {
        let html = await app.render(url, route);
      }
    
    }).listen(8080);
  • This API will be refined over time.
  • This only works in Node.js at the moment.
  • Many features will likely not work correctly, but rendering HTML at least should.

Testing

Going off of an example app at the moment. Tests will be added as features and APIs are refined.

Docs

Not yet, although it would be good to start adding some soon.

@changeset-bot
Copy link

changeset-bot bot commented Feb 8, 2022

🦋 Changeset detected

Latest commit: 21e1ac8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
astro Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: example Related to an example package (scope) pkg: preact Related to Preact (scope) pkg: integration Related to any renderer integration (scope) labels Feb 8, 2022
@matthewp matthewp marked this pull request as draft February 8, 2022 13:32
packages/astro/package.json Outdated Show resolved Hide resolved
@github-actions github-actions bot added test and removed pkg: preact Related to Preact (scope) pkg: integration Related to any renderer integration (scope) labels Feb 8, 2022
@matthewp matthewp marked this pull request as ready for review February 8, 2022 17:14
}
abstract render(req: any, routeData?: RouteData): Promise<string>;
protected async renderURL(url: URL, routeData?: RouteData): Promise<string> {
if(!routeData) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Route data is always undefined I think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I keep pressing the button

Copy link
Contributor

@boehs boehs left a comment

Choose a reason for hiding this comment

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

This code was incredibly well written, mind you I was skimming but the fact the only thing that I could find was uneeded {} and some minor readability really says something for your work.

packages/astro/src/core/render/core.ts Show resolved Hide resolved
packages/astro/src/core/render/core.ts Show resolved Hide resolved
packages/astro/src/core/render/core.ts Show resolved Hide resolved
packages/astro/src/core/config.ts Show resolved Hide resolved
packages/astro/src/core/render/dev/error.ts Show resolved Hide resolved
packages/astro/src/core/routing/validation.ts Show resolved Hide resolved
packages/astro/src/core/routing/validation.ts Show resolved Hide resolved
@@ -31,6 +31,7 @@ function printHelp() {
--project-root <path> Specify the path to the project root folder.
--no-sitemap Disable sitemap generation (build only).
--experimental-static-build A more performant build that expects assets to be define statically.
--experimental-ssr Enable SSR compilation.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix indentation here

@github-actions github-actions bot added pkg: preact Related to Preact (scope) pkg: integration Related to any renderer integration (scope) labels Feb 10, 2022
@github-actions github-actions bot removed pkg: integration Related to any renderer integration (scope) pkg: preact Related to Preact (scope) labels Feb 10, 2022
Copy link
Contributor

@tony-sull tony-sull left a comment

Choose a reason for hiding this comment

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

LGTM! Big fan of the refactoring here, especially renaming ssr to render and breaking out a few pieces to separate files

It looks like the CI tests may still be failing but I'm not 100% sure if that's just an outdated flag I'm seeing in GH. I'd say its good to merge in as long as the tests are passing 👍

@matthewp matthewp merged commit ba5e2b5 into main Feb 14, 2022
@matthewp matthewp deleted the experimental-ssr branch February 14, 2022 17:48
This was referenced Feb 14, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Checkpoint, basics are working

* Add the `--experimental-ssr` flag

* Adds the changeset

* Fixes population of getStaticPaths results

* Pass through the imported module

* Route manifest test

* Fix remaining tests

* Fix remaining tests

* Copy server assets over

* Fix types

* Allowing passing in the request to the Node version of App

* Improve the example app

* Gets CI to pass
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Checkpoint, basics are working

* Add the `--experimental-ssr` flag

* Adds the changeset

* Fixes population of getStaticPaths results

* Pass through the imported module

* Route manifest test

* Fix remaining tests

* Fix remaining tests

* Copy server assets over

* Fix types

* Allowing passing in the request to the Node version of App

* Improve the example app

* Gets CI to pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: example Related to an example package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants