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

feat(ssr): adds SSR support #199

Merged
merged 3 commits into from
Dec 1, 2021
Merged

feat(ssr): adds SSR support #199

merged 3 commits into from
Dec 1, 2021

Conversation

pavankjadda
Copy link
Collaborator

@pavankjadda pavankjadda commented Dec 1, 2021

In order to better understand this, I went through express.js tutorial. The server.ts has some basic express.js code. After debugging for few hours, I was able to identify the issue. Here is the summary

  • When express server loads web page, it does have cookies in request object, but since our cookie service depends on document object we can't access them
  • To fix this, first we need to provide REQUEST in providers. We can achieve this by modifying server.ts file
...............

  server.get('*', (req, res) => {
    res.render(indexHtml, {
      req, providers: [
        {provide: APP_BASE_HREF, useValue: req.baseUrl},
        {provide: 'REQUEST', useValue: (req)},
        {provide: 'RESPONSE', useValue: (res)}]
    });
  });
  return server;
};

...............
  • Then we need to inject REQUEST object in cookie service, which helps us to get cookies on server side through REQUEST
import {REQUEST} from '@nguniversal/express-engine/tokens';

@Injectable()
export class CookieService {
constructor(@Optional() @Inject(REQUEST) private request: Request) {
  }

  • Then we need provide this in AppServerModule
@NgModule({
  imports: [
    AppModule,
    ServerModule,
  ],
  bootstrap: [AppComponent],
  providers: [CookieService]
})
export class AppServerModule {
}

  • Finally, if the platform is server, we can get the cookies in the following way, otherwise we can use existing method
const result: RegExpExecArray = regExp.exec(this.isServer ? this.request?.headers.get('cookie') : this.document.cookie);

I checked this in my project and it works. Let me know your thoughts on this

@pavankjadda pavankjadda marked this pull request as ready for review December 1, 2021 18:01
@pavankjadda pavankjadda merged commit 1db4cd1 into stevermeister:master Dec 1, 2021
@pavankjadda pavankjadda deleted the ssr-cookie-service branch December 1, 2021 18:07
@Spawnrad
Copy link

Spawnrad commented Dec 8, 2021

@stevermeister is it possible to have a code example to implement that please?

Thank you a lot both of you for this support <3

@pavankjadda
Copy link
Collaborator Author

Yes, I am in the process of updating the docs.

@pavankjadda
Copy link
Collaborator Author

@Spawnrad you can try version 13.1.0 to test this. See the docs here and example here

@Spawnrad
Copy link

Thank you a lot <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants