-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[FrameworkBundle][HttpKernel][Routing] Static Site Generation #61531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this shouldn't belong to a third party package.
That'd be possible, isn't it? Or do you see any blocker?
If we want this in core, everything should be in HttpKernel IMHO.
There's too much in Routing currently to me. Nothing there would be better :)
* @param bool|null $stateless Whether the route is defined as stateless or stateful, @see https://symfony.com/doc/current/routing.html#stateless-routes | ||
* @param string|string[]|null $env The env(s) in which the route is defined (i.e. "dev", "test", "prod", ["dev", "test"]) | ||
* @param string|DeprecatedAlias|(string|DeprecatedAlias)[] $alias The list of aliases for this route | ||
* @param bool|array{params?: string|iterable<array<mixed>>}|null $staticGeneration The static generation configuration, params : route parameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we already have $defaults
for defining default route parameters
and we also have $stateless for identifying routes that could trigger static page generation
can't we rely on those? that'd reduce complexity IMHO
continue; | ||
} | ||
|
||
if ([] !== $route->getMethods() && !\in_array(Request::METHOD_GET, $route->getMethods(), true)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all comparisons to bool/empty values should be simplified in the PR - those are needless boilerplate:
if ([] !== $route->getMethods() && !\in_array(Request::METHOD_GET, $route->getMethods(), true)) { | |
if ($route->getMethods() && !\in_array(Request::METHOD_GET, $route->getMethods(), true)) { |
$compiledRoute = $route->compile(); | ||
|
||
// $config "true" means no params to match the path variables | ||
if ([] === $compiledRoute->getPathVariables() || true === $config) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
etc
if ([] === $compiledRoute->getPathVariables() || true === $config) { | |
if (!$compiledRoute->getPathVariables() || $config) { |
throw new LogicException(\sprintf('Expected route "%s" to accept GET method, it accepts "%s" only.', $routeName, implode(', ', $route->getMethods()))); | ||
} | ||
|
||
if (false === ($route->getDefaults()['_stateless'] ?? false)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (false === ($route->getDefaults()['_stateless'] ?? false)) { | |
if (!($route->getDefaults()['_stateless'] ?? false)) { |
Sure, it already exists as Stenope Bundle I think implementing the basic SSG feature in core could be useful for this kind of bundle. I also think SSG is a must-have feature for a modern web framework. But that's totally up to you, you have a much better vision of Symfony's goals and future than me :D
I'll think about it, but some configuration are required, |
in the long run it's minus "basic" ;) i think it's a good feature; but im not yet convinced somehow |
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 :
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:

StaticPageUrisProvider
lists all Route marked as static, those can be defined as follows :staticGeneration
configuration can be :routing.static_site.params_provider
)There are some extension points :
StaticPageUrisProviderInterface
to provide static page by another mean thansymfony/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, ...