Angular服务器端渲染(SSR)不加载内 /子页面的HTML

发布于 2025-01-28 19:18:23 字数 998 浏览 6 评论 0原文

我的角度应用程序如何加载了一个问题。由于某种原因,无论路线如何,在线查看应用程序,HTML结构默认为基本路径。

现在假设在我的路由中,我有2页:HOME (/)和Events (/Events)

当我尝试使用Facebook Open Graph scrapper,甚至只是简单地view> view-source:https:// mysite/events我看到结果是主页(/)而不是/events

更令人困惑的是,

  1. dist> dist目录中的实际渲染/events html文件具有正确的HTML数据如上所述,它在线查看或取消,它显示了主页html结构,
  2. 即使加载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

  1. the actual rendered /events html file in the dist directory has the correct html data but on viewing it online or scrapping as mentioned, it shows the homepage html structure
  2. even on loading https://mysite/events on the browser - which shows the intended page correctly, but upon inspecting by ctrl + 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独守阴晴ぅ圆缺 2025-02-04 19:18:23

我认为您可以在HOMECONTENT旁边使用EventComponent路线。

const routes: Routes = [
  {
    path: '',
    component: SiteLayoutComponent,
  },
  {
    path: 'events',
    component: EventsComponent,
  },
  { path: '**', redirectTo: '' }
];

I think you can use EventComponent route, beside HomeComponent.

const routes: Routes = [
  {
    path: '',
    component: SiteLayoutComponent,
  },
  {
    path: 'events',
    component: EventsComponent,
  },
  { path: '**', redirectTo: '' }
];
违心° 2025-02-04 19:18:23

我在我的本地Apache服务器以及另一个站点(原来是使用Apache)上测试了Web应用程序,并且在这两种情况下,该路由都可以正常工作。

因此,事实证明,该问题是nginx将请求重定向到根索引(然后在请求页面上),而不是直接转到子目录索引。

因此,如果您有一个请求,例如site.com/events - on nginx请求将加载Main index.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 - on nginx the request would load the main index.html then programmatically load the user's request (/events) whereas on apache 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文