Open efc opened 2 weeks ago
With some help at the Statamic discord I was able to manage this myself. I am sharing it here in case you want to build it in to the addon as well.
I added the following code to the boot()
function in my /app/Providers/AppServiceProviders.php
file...
SSG::after(function () {
$dest = config('statamic.ssg.destination');
$base = rtrim(config('statamic.ssg.base_url'), '/'); // remove trailing slash
$fs = app('files');
$redirects = $fs->files('content/alt-redirect');
print("Found " . count($redirects) . " redirects\n");
$generated = $directories = 0;
foreach( $redirects as $redirect ) {
$yaml = $fs->get($redirect->getPathname());
if ( $yaml ) {
$r = Yaml::parse($yaml);
$from_dir = $dest . $r['from'];
$from = $from_dir . 'index.html';
$to = $base . $r['to'];
if ($fs->missing($from)) {
$contents = <<<HTML
<html>
<head>
<meta http-equiv="Refresh" content="0; url='$to'" />
</head>
<body>
<p>Moved to <a href="$to">$to</a></p>
</body>
</html>
HTML;
if (!is_dir($from_dir)) {
mkdir($from_dir, 0777, true);
$directories++;
}
if ($fs->put($from, $contents)) {
$generated++;
}
}
}
}
print("Generated $generated redirect files in $directories new directories\n");
});
I also added these two includes at the top of that same file...
use Statamic\StaticSite\SSG;
use Symfony\Component\Yaml\Yaml;
I'm sure there may be ways to clean this up, since I've never written any code for Laravel or Statamic before. For example, this code ignores redirect types and the site field of each redirect. But this worked for me, so I wanted to share it.
Ello ello!
Thanks for this! We'll have a gander at this the next time we're on it!
How can I use Alt-Redirect to generate static redirects with the Static Site Generator from Statamic?
In other words, I would like the SSG to create HTML files that match the "from" path of the redirect (including uppercase, when specified in the "from") with "refresh" meta in the head of those files matching the "to" of the redirect.
Is there some way I can "template" this for the SSG using Alt-Redirect content?