Angular服务器端渲染(SSR)不加载内 /子页面的HTML
我的角度应用程序如何加载了一个问题。由于某种原因,无论路线如何,在线查看应用程序,HTML结构默认为基本路径。
现在假设在我的路由中,我有2页:HOME (/)
和Events (/Events)
。
当我尝试使用Facebook Open Graph scrapper,甚至只是简单地view> view-source:https:// mysite/events
我看到结果是主页(/)而不是
/events
更令人困惑的是,
dist> dist
目录中的实际渲染/events
html文件具有正确的HTML数据如上所述,它在线查看或取消,它显示了主页html结构,- 即使加载
https:// mysite/events 在浏览器上 - 它正确地显示了预期的页面,但在通过
ctrl进行检查时+ u
,同一问题-HTML结构属于主页
是否与Angular加载应用程序的加载或其他内容有关?我该如何确保由刮擦者甚至通过查看源来挑选正确的HTML数据?
万一路由信息可能会有所帮助,在这里是:
const routes: Routes = [
{
path: '',
component: SiteLayoutComponent,
children: [
{
path: 'events',
children: [
{ path: '', component: EventsComponent},
]
},
{ path: '', component: HomeComponent, pathMatch: 'full'},
]
},
{ path: '**', redirectTo: '' }
];
I'm experiencing an issue with how my angular app loads. For some reason on viewing the application online regardless of the route, the html structure defaults to the base path.
Now assume in my routing I have 2 pages: home (/)
and events (/events)
.
When I try to use the facebook open graph scrapper or even just simply view-source:https://mysite/events
I see the results are that of the homepage (/)
instead of the /events
Even more confusing is that
- the actual rendered
/events
html file in thedist
directory has the correct html data but on viewing it online or scrapping as mentioned, it shows the homepage html structure - even on loading
https://mysite/events
on the browser - which shows the intended page correctly, but upon inspecting byctrl + u
, same issue - html structure belonging to the homepage
Does this have anything to do with how angular loads the app or it's something else? How can I go about ensuring the correct html data is picked up by scrappers or even just by viewing the source?
Just in case the routing info might help, here it is:
const routes: Routes = [
{
path: '',
component: SiteLayoutComponent,
children: [
{
path: 'events',
children: [
{ path: '', component: EventsComponent},
]
},
{ path: '', component: HomeComponent, pathMatch: 'full'},
]
},
{ path: '**', redirectTo: '' }
];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以在HOMECONTENT旁边使用EventComponent路线。
I think you can use EventComponent route, beside HomeComponent.
我在我的本地Apache服务器以及另一个站点(原来是使用Apache)上测试了Web应用程序,并且在这两种情况下,该路由都可以正常工作。
因此,事实证明,该问题是
nginx
将请求重定向到根索引(然后在请求页面上),而不是直接转到子目录索引。因此,如果您有一个请求,例如
site.com/events
- onnginx
请求将加载Mainindex.html
,然后将请求(/events
),而在apache
上,它将直接进入/events
中找到的索引这说明了为什么像开放图形刮刀或甚至
ctrl + u
都会找到主索引文件中的元标记数据,而不是事件索引文件I tested the web application on both my local apache server as well as on another site (which turned out to be using apache) and the routing works fine on both scenarios.
So the issue turned out to be an
nginx
configuration that redirected requests to the root index (then on to the requested page) rather than go straight to the sub directory index.So if you have a request like
site.com/events
- onnginx
the request would load the mainindex.html
then programmatically load the user's request (/events
) whereas onapache
it would go straight to the index found in/events
This explains why scrappers like the open graph scrapper or even
ctrl + u
would find meta tag data that's in the main index file rather than the events index file