Skip to content

Commit

Permalink
Headless mode url rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
martink635 committed Nov 10, 2023
1 parent 57adb8d commit 3821466
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ It works similary to the Antlers tags, so you can use single values as well.

Headless use is straightforward. If using the REST API or GraphQL, the entry will include three Seotamic fields: `seotamic_meta`, `seotamic_social` with the prefilled SEO data.

You can also set the base canonical url in the config file by setting a value for `headless_mode`. The default value is `false`, but setting it to `https://mydomain.com` will change all canonical urls. The images will NOT use this base url, as they are still served from Statamic.

**Headless usage is supported only for the PRO version.**

## Sitemap (PRO)
Expand Down
7 changes: 7 additions & 0 deletions config/seotamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
// Do we want to add a /sitemap.xml to the site?
'sitemap' => true,

// If set (ie https://test.domain.com) this base url will replace the
// app url for canonical and social url tags. Image links will still
// be linked to the CMS domain.
//
// Defautl value: false
'headless_mode' => false,

// Default recommended field lengths.
// They are just guidelines and can be ignored on the frontend
'meta_title_length' => 60,
Expand Down
25 changes: 15 additions & 10 deletions src/FieldTypes/SeotamicMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function preload()
'label_custom' => __('seotamic::seo.meta_label_custom'),
'label_empty' => __('seotamic::seo.meta_label_empty'),
'preview_title' => __('seotamic::seo.meta_preview_title'),

]
];
}
Expand Down Expand Up @@ -101,28 +100,34 @@ protected function getCanonical(): string
return "";
}

$url = $this->field->parent()->permalink;
$uri = $this->field->parent()->uri;
$config = config('seotamic');
$base_url = env('APP_URL');

if (isset($config['headless_mode']) && $config['headless_mode'] !== false) {
$base_url = $config['headless_mode'];
}

// remove trailing slash from base url
if (substr($base_url, -1) === '/') {
$base_url = substr($base_url, 0, -1);
}

// First child option can return 404 if there is no first child
if ($this->field->parent()->value('seotamic_canonical') !== null) {
$url = $this->field->parent()->value('seotamic_canonical');

// We have to make sure the given url is formatted correctly
// If it's a relative path it must have a / prepended
// And we expect the .env APP_URL doesn't
if (substr($url, 0, 4) !== 'http') {
$appUrl = env('APP_URL');

if (substr($appUrl, -1) === '/') {
$appUrl = substr($appUrl, 0, -1);
}

if (substr($url, 0, 1) !== '/') {
$url = '/' . $url;
}

$url = $appUrl . $url;
$url = $base_url . $url;
}
} else {
$url = $base_url . $uri;
}

return $url ?? "";
Expand Down
7 changes: 0 additions & 7 deletions src/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,6 @@ protected function formBlueprint()
'display' => __('seotamic::general.settings_preview_domain_title'),
'instructions' => __('seotamic::general.settings_preview_domain_instructions'),
],
'headless_mode' => [
'type' => 'text',
'character_limit' => '50',
'prepend' => 'https://',
'display' => "Headless Mode",
'instructions' => "If set this domain will replace the domain for canonical and social url tags. Image links will still be linked to the CMS domain.",
],
'robots_none' => [
'type' => 'toggle',
'display' => __('seotamic::general.settings_robots_title'),
Expand Down

0 comments on commit 3821466

Please sign in to comment.