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

Prerender does not work with templateUrl or styleUrls #294

Closed
manekinekko opened this issue Feb 28, 2016 · 36 comments
Closed

Prerender does not work with templateUrl or styleUrls #294

manekinekko opened this issue Feb 28, 2016 · 36 comments

Comments

@manekinekko
Copy link
Contributor

@jongood01 has reported this issue in the universal-starter

Components that have separate html and css files and reference them via templateUrl and styleUrls do not prerender but client-side still loads i.e. this doesn't work:

@Component({
  selector: 'app',
  templateUrl: 'path/to/my/template.html',
  styleUrls: ['path/to/my/stylesheet.css'],
})
But this does:

@Component({
  selector: 'app',
  template: `
<div>Here is my template</div>
`,
  styles: [`
   .classy {
        background: red;
   }
`],

see issue here: angular/universal-starter#43

@Bhushan001
Copy link

@manekinekko has this issue been resolved?

@PatrickJS
Copy link
Member

it shouldn't be resolved. the issues is that on the server the url needs to be a full url

@jesussobrino
Copy link

I was getting crazy before I read this issue. I'm subscribing waiting for news.
Thanks for a great work.

@creaux
Copy link

creaux commented Mar 16, 2016

Hah, exactly. Webpack seems to be the solution in the meantime.

On 16 Mar 2016, at 12:29, Jesús Sobrino notifications@github.com wrote:

I was getting crazy before I read this issue. I'm subscribing waiting for news.
Thanks for a great work.


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub

@Bhushan001
Copy link

@gdi2290 i tried using full url as templateUrl: 'http://localhost/app/app.component.html' but no success.

@helenitvn
Copy link

Try using this instead @Bhushan001:
templateUrl: '/app/app.component.html'

@Bhushan001
Copy link

@helenitvn @gdi2290 I tried templateUrl: 'app/app.component.html'
after that I was getting this error:
EXCEPTION: Failed to load app/app.component.html ngOnInit Error: Failed to load app/app.component.html Async Error: Failed to load app/app.component.html ngOnStable Error: Failed to load app/app.component.html preboot Error: Failed to load app/app.component.html Rendering Document Error: Failed to load app/app.component.html ngOnRendered Error: Failed to load app/app.component.html

@helenitvn
Copy link

@Bhushan001: Could you show me your project structure?

It works in my case.

templateUrl

@Bhushan001
Copy link

@helenitvn in case of normal angular2 app it works.but I am trying to use it with universal-starter
directory structrure

@helenitvn
Copy link

I also use universal-starter as my seed, too.

In your case you need to use templateUrl: '/src/app/app.component.html' instead templateUrl: '/app/app.component.html'

@Bhushan001
Copy link

@helenitvn its still not working but this worked for me.
templateUrl: 'http://localhost:3000/src/app/app.component.html'

@jeremythuff
Copy link

For me templateUrl: '/src/app/view/dashboard.component.html' is not working. It throws "EXCEPTION: Error: Uncaught (in promise): Failed to load /src/app/view/dashboard.component.html"

templateUrl: 'http://localhost:3000/src/app/view/dashboard.component.html' is grabbing the template but not waiting on asynchronous content before rendering.

@Bhushan001
Copy link

@gdi2290 @jeffwhelpley @jeremythuff @helenitvn previously
templateUrl: 'http://localhost:3000/src/app/app.component.html' worked for me previously to use external html but its not working anymore.I mean its not giving any error but the template is not getting rendered in app.
generated html of my app
please have a look at my repository for code.Universal-Starter
Thanks in advance

@Bhushan001
Copy link

@gdi2290 @jeffwhelpley any inputs?

@jeffwhelpley
Copy link
Contributor

@gdi2290 my head is swiss cheese at this point, but didn't we have a change to BASE_URL recently that may have affected this?

@justindujardin
Copy link
Contributor

I posted a workaround for this issue in #346 which is published with >=0.86.0. I'm not sure if it will work in all cases, but it worked for my project.

You can set a custom URL resolver to change your template/style urls to be file:// based, then the NodeXHRImpl will load them from disk using fs.readFile instead of making a network request.

My resolver looks like this:

export class NodeUrlResolver extends UrlResolver {
  resolve(baseUrl: string, url: string): string {
    return `file://${path.resolve(path.join(baseUrl, url))}`;
  }
}

and I provide it during prerender with:

provide(UrlResolver, {useValue: new NodeUrlResolver}),

@Bhushan001
Copy link

@jeffwhelpley @gdi2290 has any fix released yet?

@PatrickJS
Copy link
Member

@Bhushan001 can you update to angular2-universal 0.91.0

@Bhushan001
Copy link

@gdi2290 i installed the angular2-universal module and changed all imports from angular2-universal-preview to angular2-universal, now my app is broken

@PatrickJS
Copy link
Member

@Bhushan001
Copy link

@gdi2290 any ideas on when this feature will be available ?

@PatrickJS
Copy link
Member

PatrickJS commented Apr 22, 2016

I need to update zone.js for it to track fs and http/https

@GreenToast
Copy link

It works in my project with the external html being rendered server-side. It works always fine for directives outside the router-outletm, but inside the router-outlet it works only on the first serve. After a reload, even a hard reload or a load in another browser the prerendered-html of the router-outlet is missing.

The example lies here.

@Bhushan001
Copy link

@gdi2290 any success yet?

@Bhushan001
Copy link

@gdi2290 @jeffwhelpley we are working on a project in which this feature is required.I was wondering If u could provide any inputs on when this feature will be available?

@jeffwhelpley
Copy link
Contributor

Sorry for the delay in response @Bhushan001. Last week was crazy with ng-conf and we are just starting to get back into things. So, the status here is that we know what needs to change (modifications to the zones patch) and I have it right near the top of our overall priority list.

@gdi2290 can you provide an update once you get the fix in for this?

@vinodbhargava
Copy link

Hi @gdi2290 , did you get a chance to look into this?

@jeffwhelpley
Copy link
Contributor

@vinodbhargava Patrick is working on this right now. This is one of out two top priorities right now as it is roadblocking several other things. You can expect to hear an update on this within the next week.

@durbonas
Copy link

durbonas commented Jun 4, 2016

This worked for me:

My component that contains my app ('my-app') in the index page looks like this:
@Component({ moduleId: module.id, selector: 'my-app', templateUrl: 'app.component.html', styleUrls: ['app.css','font-awesome.min.css'], directives: [ROUTER_DIRECTIVES] })

Every other component within that I navigate to looks like this:
@Component({ selector: 'login', templateUrl: 'app/login.component.html', styleUrls: ['app/app.css', 'app/font-awesome.min.css'] })

adding app/ seems to do the trick and my styles appear.

@durbonas
Copy link

durbonas commented Jun 4, 2016

@gdi2290 @jeffwhelpley try "app/" not "/app/" - look at my comment above.

@PatrickJS
Copy link
Member

for templateUrl you need to __filename for moduleId here's an example
https://github.com/angular/universal-starter/blob/master/src/app/home/home.component.ts#L4

styleUrls doesn't work though so I need to look into that

@davidthor
Copy link

I've been following this thread closely, and was just checking in to see if there's an update. Haven't gotten any of the proposed work-arounds to work as of yet, and would love to know more about the proposed patches coming down the pipeline.

Thanks in advance.

@PatrickJS
Copy link
Member

for webpack use angular2-template-loader. it's setup in the universal-starter

@davidthor
Copy link

Thanks for the response @gdi2290. I'm realizing now that I might be looking at the problem wrong if the resolution is as simple as that. I started from the universal-starter and thus have angular2-template-loader in place, but didn't want to be stuck leveraging the router-deprecated, so I updated to the latest and made the relevant modifications to the route configuration. I thought I had read elsewhere that the latest version of angular-universal was supportive of the new router, but it's possible that my issues are related to those updates instead of explicitly to the templateUrl locations. I may have leapt to conclusions since I was experiencing errors server-side but not client-side.

@PatrickJS
Copy link
Member

track #432 for runtime styleUrls. for runtime templateUrl provide __filename for moduleId

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests