简体   繁体   English

如何正确处理 Next.js 中动态路由的 SSG 分页?

[英]How to correctly handle SSG pagination with dynamic routes in Next.js?

I'm fairly new to Next as a framework and I'm trying to determine how best to handle pagination.作为一个框架,我对 Next 相当陌生,我正在尝试确定如何最好地处理分页。 My goal is to have urls such as:我的目标是拥有以下网址:

  • /category-one /第一类
  • /category-one/2 /类别一/2
  • /category-one/3 /第一类/3
  • /category-two /第二类
  • /category-two/2 /类别二/2
  • ... and so on ... 等等

* notice the lack of a /1 *注意缺少 /1

Next appears to operate through dynamic slugs in the folder/filename eg Next 似乎通过文件夹/文件名中的动态 slug 进行操作,例如

pages/  
  [category]/  
    [page].jsx  

However, what I'm struggling to work out, is that how I can have a page template for NON-paginated urls, and one for paginated urls, without essentially copying the file, this ending up with a bunch of extra code to maintain.但是,我正在努力解决的问题是,我如何为非分页 url 提供一个页面模板,以及为分页 url 提供一个页面模板,而无需复制文件,这最终需要维护一堆额外的代码。

My first thought was to see if I could have say:我的第一个想法是看看我是否可以说:

pages/
  [page].jsx  
  [category]/  
    [page].jsx  

and extend the parent page for the child page, and just add some littles bits to handle the second part of the slug.并为子页面扩展父页面,并添加一些小位来处理 slug 的第二部分。

However, this doesn't seem to be an option.但是,这似乎不是一个选择。 I feel like I'm missing something obvious, but I can't seem to find any examples that show a similar setup to what I'm seeking.我觉得我遗漏了一些明显的东西,但我似乎找不到任何显示与我正在寻找的设置类似的示例。

I appreciate that the simple answer would be "just have a /1 after the page one and forget the parent page template altogether", but I'd expect that a framework as robust as Next.js would be able to handle something simple like this.我很欣赏简单的答案是“在第一页之后有一个 /1 并完全忘记父页面模板”,但我希望像 Next.js 这样强大的框架能够处理像这样简单的事情。

Any help/pointers would be greatly appreciated!任何帮助/指针将不胜感激!

For anybody finding this question and having the same problem, the answer (which was provided by juliomalves in the comments) is exactly what was missing.对于任何发现此问题并遇到相同问题的人,答案(由juliomalves在评论中提供)正是缺少的。

Look into optional catch-all routes .查看可选的包罗万象的路线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM