#Static pages on a server rendered site

4 messages · Page 1 of 1 (latest)

tepid oxide
#

I have a server rendered site with output: "server". I'd like to bring a bunch of static content in from another site with output: "static". Can I do that? Can I still use getStaticPaths()? Does that render out to HTML alongside the server functions?

hidden pollenBOT
#

Hi @tepid oxide. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

You can mix static and server-rendered routes in the same Astro project, even when output: "server" is set.

1. output: "server" just changes the default

With output: "server", all routes are rendered on demand by default. You can then opt specific routes back into prerendering by exporting prerender = true in those files. [output config; on-demand rendering]

// src/pages/about.astro
---
export const prerender = true;
---
<!-- This page will be built as static HTML even in server mode -->

2. You can still use getStaticPaths() in server mode

getStaticPaths() is specifically for prerendered routes. In server mode, you can still use it, but only on routes that are actually prerendered (i.e. where prerender is true). [getStaticPaths]

// src/pages/posts/[id].astro
---
export const prerender = true; // opt this route into static generation

export async function getStaticPaths() {
  return [
    { params: { id: '1' } },
    { params: { id: '2' } },
  ];
}

const { id } = Astro.params;
---
<h1>{id}</h1>

Those paths will be built to HTML at build time, alongside your server entry output.

3. How this looks in your “mixed” setup

#
  • Your main project is configured with output: "server".
  • Any routes you want to “bring in” as static content from the other site:
    • Recreate them as pages/endpoints in this project.
    • Mark them with export const prerender = true.
    • Use getStaticPaths() if they are dynamic routes and you want multiple static pages. [routing prerender; content collections example]

Astro will then:

  • Emit server code for your on-demand routes.
  • Emit static HTML (and any related assets) for the prerendered routes, even though the overall output mode is server.

So yes: you can still use getStaticPaths(), and those routes will be rendered out to static HTML alongside your server-rendered routes, as long as they are marked to be prerendered.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: