Pagination page directories missing 1

I’m working on making a JSON rest-like api with static files and I have added pagination like:

{{ $p := .Paginate .Pages.ByDate.Reverse }}

when I run hugo I see the pages folder generated but it starts at 2 and the page 1 json is outside that name by its type .json. I can’t seem to find why its not being consistent with a folder for page 1 as well. Am I missing something in the settings?

You will need to add a little bit more information and code samples, but I think what Hugo creates is this:

/path/to/something   <-- page 1
/path/to/something/2 <-- page 2
/path/to/something/3 <-- page 3

I would expect that this way if your “API endpoint” is a .md file and not a list page. I would also assume, that Hugo somehow has redirect included for people trying to load /path/to/something/1 one level up.

That is the output I am seeing, page one is outside the page directory on its own and its the first list page. I do not get a redirect generated, probably because its json output only. Its not that this is bad for the first page, I guess I was thinking it would be nice if it could also have the /page/1/something.json for consistency. If there is no option to cause the /page/1 folder to be generated then I’ll work around it or possibly set up a redirect. There is not too much going on in the list template so far:

{{- $list := slice -}}
{{ $p := .Paginate .Pages.ByDate.Reverse }}
{{- range $p.Pages -}}
  {{- .Render "li" -}}
  {{- $list = $list | append (slice (.Scratch.Get "item")) -}}
{{- end -}}
{{- $list | jsonify -}}

Depending on your hoster you could “proxy” the URL. On Netlify it would be something like this in the _redirects file in your site root:

/path/to/something/1            /path/to/something 200

The 200 will result in the browser still showing the /1 subdirectory. That will work only on Netlify though… Other server types or hosting services might have another way.

Thanks!

Hugo already does the above at least for HTML Output, not sure about JSON, but you can try.

For example in the demo of the Ananke theme if a user types:
https://gohugo-ananke-theme-demo.netlify.app/post/page/1/
they will be redirected to
https://gohugo-ananke-theme-demo.netlify.app/post/

I think it is wanted the other way around. Is there a way to disable the “root” directory version for a page/1` forced version?

Not from what I know.

1 Like

I’d even be ok with it generating both the root directory version and the /page/1 version