Skip to content

Conversation

@Kocal
Copy link
Collaborator

@Kocal Kocal commented Feb 13, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

This PR allows a route to be defined as "static generated".

A static generated route is dumped on a storage thanks to a Symfony command: static-site-generation:generate.
With a simple NGINX / Caddy configuration, the generated files could be served directly.
Example with Caddy :

{$SERVER_NAME:localhost} {
	root * /app/public
	encode zstd gzip

	{$CADDY_SERVER_EXTRA_DIRECTIVES}

	try_files {path} /static-pages{path}.html
	try_files {path} /static-pages{path}

	php_server
}

A good use case for this feature is a blog, where pages almost never change.
In that case, if the route is configured as static generated, Symfony won't even load, as the server serves the generate page directly (which implies a huge performance boost).

Similar implementations can be found in Tempest, Astro, and many other frameworks.

Here is an overview of the feature architecture:
ssg

StaticPageUrisProvider lists all Route marked as static, those can be defined as follows :

#[Route(name: 'app_sitemap', path: 'sitemap.xml', methods: ['GET'], format: 'xml', staticGeneration: true)]
public function __invoke(): Response
// ...

staticGeneration configuration can be :

  • a boolean
#[Route(name: 'home', path: '/', staticGeneration: true)]
  • a static array
#[Route(name: 'restaurant_detail', path: '/restaurant/{city}', staticGeneration: [['city' => 'lyon'], ['city' => 'hanoi']])]
  • a service ID (tagged with routing.static_site.params_provider)
#[Route(name: 'product_detail', path: '/product/{slug}', staticGeneration: ProductSlugProvider::class)]

There are some extension points :

  • StaticPageUrisProviderInterface to provide static page by another mean than symfony/routing. A third party CMS system for example.
  • ParamsProviderInterface to resolve params of dynamic routes. For example, with a route /blog/{slug}. We can create a param providers to list all slugs from a database, or all Markdown files in a directory.
  • StaticPageDumperInterface to dump the pages content on another storage. Can be useful if we need to dump it to an S3 storage, to GitHub pages, ...

@Kocal Kocal force-pushed the static-site-generation branch from 432ae67 to 3ad1e9a Compare February 13, 2025 12:09
@tBibaut tBibaut force-pushed the static-site-generation branch from 3749894 to 8e29cba Compare February 20, 2025 16:31
@tBibaut tBibaut force-pushed the static-site-generation branch 2 times, most recently from 405b576 to 2e25ef3 Compare March 14, 2025 16:03
@tBibaut tBibaut force-pushed the static-site-generation branch from 2e25ef3 to af81029 Compare March 30, 2025 15:10
@tBibaut tBibaut force-pushed the static-site-generation branch from af81029 to 7816ddf Compare June 19, 2025 11:52
@tBibaut tBibaut force-pushed the static-site-generation branch 3 times, most recently from 72f1933 to d19d15a Compare August 26, 2025 12:50
@tBibaut tBibaut changed the base branch from 7.3 to 7.4 August 26, 2025 12:51
@tBibaut tBibaut force-pushed the static-site-generation branch from d19d15a to c75a434 Compare August 26, 2025 13:52
@mtarld mtarld changed the title Implement Static Site Generation (SSG) feature [Routing] [HttpKernel] [FrameworkBundle] Static Site Generation Aug 26, 2025
@tBibaut tBibaut force-pushed the static-site-generation branch 5 times, most recently from ac75f50 to a02e805 Compare August 26, 2025 15:12
@tBibaut tBibaut force-pushed the static-site-generation branch from a02e805 to a9e04cf Compare August 26, 2025 15:16
@mtarld mtarld closed this Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants