Code Monkey home page Code Monkey logo

Comments (5)

JeanMeche avatar JeanMeche commented on April 28, 2024

Hi, this issue is being tracked at angular/angular-cli#26323

from angular.

alexcmoss avatar alexcmoss commented on April 28, 2024

There is an issue with your example.

'query-params' !== new InjectionToken('query-params')

Try creating a single injection token instance and using it in both AppComponent and server.ts as below.

in app.component.ts

export const QUERY_PARAMS = new InjectionToken('query-params');
...
@Optional() @Inject(QUERY_PARAMS)
              public queryParams: { email: string, name: string, lang: string }

in server.ts

import { QUERY_PARAMS } from './src/app/app.component';

// pre-render hooks
  server.get('*', (req, res, next) => {
    const { protocol, originalUrl, baseUrl, headers } = req;
    queryParams = req.query;
    commonEngine
      .render({
        bootstrap,
        documentFilePath: indexHtml,
        url: `${protocol}://${headers.host}${originalUrl}`,
        publicPath: browserDistFolder,
        providers: [
          { provide: APP_BASE_HREF, useValue: baseUrl },
          {
            // provide: 'query-params',
            provide: QUERY_PARAMS,
            useValue: queryParams
          }
        ]
      })
      .then((html) => {
        console.log(html);
        res.send(html);
      })
      .catch((err) => next(err));
  });

from angular.

BeCaDRI avatar BeCaDRI commented on April 28, 2024

@alexcmoss thanks, but this is still not working using the latest Angular 17 SSR. I've tried it with APP_BASE_HREF or REQUEST and RESPONSE to make sure that not only custom token providers are working - same result

As far as I read and understand you have to use the extra Angular 16 builder for browser and server in angular.json to make it work?!
To reproduce just create a new Angular SSR project with Angular CLI and try to use any provider in app.component.
It is just not working - no matter how I try to use providers or how I configure my app.
As a workaround you can fetch the data on client from the node backend with an API route, but this breaks the whole intention to use specific url params to identify which user/email we have to handle...

from angular.

gnibeda avatar gnibeda commented on April 28, 2024

Is there any news? InjectionToken not working in Angular 17 SSR. If someone have working demo, please share.
It not works even if you build app and serve SSR as usual. Simply all InjectionTokens are ignored.

Update: I've solved this issue for my case. More details here: angular/angular-cli#26323 (comment)

from angular.

angular-automatic-lock-bot avatar angular-automatic-lock-bot commented on April 28, 2024

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

from angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.