updated redirect

This commit is contained in:
becarta
2025-02-11 02:51:53 +01:00
parent 282d5764ff
commit 26365cb780
5 changed files with 56 additions and 34 deletions

View File

@@ -1,7 +1,8 @@
import path from 'path'; import path from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { defineConfig } from 'astro/config'; import { defineConfig } from 'astro/config';
import netlify from "@astrojs/netlify/functions"
import sitemap from '@astrojs/sitemap'; import sitemap from '@astrojs/sitemap';
import tailwind from '@astrojs/tailwind'; import tailwind from '@astrojs/tailwind';
import mdx from '@astrojs/mdx'; import mdx from '@astrojs/mdx';
@@ -9,7 +10,6 @@ import partytown from '@astrojs/partytown';
import icon from 'astro-icon'; import icon from 'astro-icon';
import compress from 'astro-compress'; import compress from 'astro-compress';
import type { AstroIntegration } from 'astro'; import type { AstroIntegration } from 'astro';
import astrowind from './vendor/integration'; import astrowind from './vendor/integration';
import { readingTimeRemarkPlugin, responsiveTablesRehypePlugin, lazyImagesRehypePlugin } from './src/utils/frontmatter'; import { readingTimeRemarkPlugin, responsiveTablesRehypePlugin, lazyImagesRehypePlugin } from './src/utils/frontmatter';
@@ -21,8 +21,12 @@ const whenExternalScripts = (items: (() => AstroIntegration) | (() => AstroInteg
hasExternalScripts ? (Array.isArray(items) ? items.map((item) => item()) : [items()]) : []; hasExternalScripts ? (Array.isArray(items) ? items.map((item) => item()) : [items()]) : [];
export default defineConfig({ export default defineConfig({
output: 'server', output: 'static',
adapter: netlify(),
i18n: {
locales: ["es", "en", "pt-br"],
defaultLocale: "en",
},
integrations: [ integrations: [
tailwind({ tailwind({

View File

@@ -1,6 +1,2 @@
/_astro/* /_astro/*
Cache-Control: public, max-age=31536000, immutable Cache-Control: public, max-age=31536000, immutable
/*
Access-Control-Allow-Origin: *
Accept-Language: *

View File

@@ -1 +0,0 @@
/* /api/:splat 200

View File

@@ -1,2 +1,3 @@
User-agent: * User-agent: *
Disallow: Disallow:
Sitemap: https://www.365devnet.eu/sitemap-0.xml

View File

@@ -1,29 +1,8 @@
---
const supportedLangs = ['en', 'nl', 'de'];
let chosenLang = 'en';
// Get the Accept-Language header from the request
const acceptLang = Astro.request.headers.get('accept-language');
if (acceptLang) {
const preferredLang = acceptLang.split(',')[0].trim().split('-')[0];
if (supportedLangs.includes(preferredLang)) {
chosenLang = preferredLang;
}
}
// Perform the redirect
Astro.redirect(`/${chosenLang}`);
---
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<!-- Automatically refresh to the target URL immediately -->
<meta http-equiv="refresh" content={`0; url=${chosenLang}`} />
<title>Redirecting...</title> <title>Redirecting...</title>
<!-- Optional: Add minimal inline CSS or Tailwind via your build for a nicer look -->
<style> <style>
/* Example minimal styling */ /* Example minimal styling */
body { body {
@@ -43,13 +22,56 @@ Astro.redirect(`/${chosenLang}`);
<body> <body>
<div class="container"> <div class="container">
<h1 class="text-2xl font-bold mb-4">Redirecting...</h1> <h1 class="text-2xl font-bold mb-4">Redirecting...</h1>
<p> <p id="redirect-message">
You are being redirected to the <strong>{chosenLang.toUpperCase()}</strong> version of our site. You are being redirected to the <strong></strong> version of our site.
</p> </p>
<p class="mt-4"> <p class="mt-4">
If you are not redirected automatically, please If you are not redirected automatically, please
<a href={chosenLang} class="text-blue-500 underline">click here</a>. <a id="redirect-link" href="" class="text-blue-500 underline">click here</a>.
</p> </p>
</div> </div>
<script type="module">
// Define the supported languages
const supportedLangs = ['en', 'nl', 'de'];
let chosenLang = 'nl'; // Default language
// Get the user's language from the browser
const userLang = navigator.language || (navigator.languages && navigator.languages[0]);
if (userLang) {
// For example, "en-US" becomes "en"
const preferredLang = userLang.split('-')[0];
if (supportedLangs.includes(preferredLang)) {
chosenLang = preferredLang;
}
}
// Construct the target URL based on the chosen language
const targetURL = `/${chosenLang}`;
console.log("Target URL:", targetURL);
// Update the DOM with the computed language values:
// Update the redirect message to display the chosen language.
const messageEl = document.getElementById('redirect-message');
if (messageEl) {
messageEl.innerHTML = `You are being redirected to the <strong>${chosenLang.toUpperCase()}</strong> version of our site.`;
}
// Update the href for the clickable link.
const linkEl = document.getElementById('redirect-link');
if (linkEl) {
linkEl.href = targetURL;
}
// Option 1: Dynamically add a meta refresh tag.
const metaRefresh = document.createElement("meta");
metaRefresh.httpEquiv = "refresh";
metaRefresh.content = `0; url=${targetURL}`;
document.head.appendChild(metaRefresh);
// Option 2: Alternatively, use JavaScript redirection:
// window.location.href = targetURL;
</script>
</body> </body>
</html> </html>