Skip to content

Commit

Permalink
Merge pull request #2965 from peter-kuhmann/add-cors-header-for-track…
Browse files Browse the repository at this point in the history
…er-script

Add "Access-Control-Allow-Origin" header for tracker script.
  • Loading branch information
mikecao authored Oct 26, 2024
2 parents b2a1b17 + fec0701 commit 1f52b49
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const contentSecurityPolicy = [
`frame-ancestors 'self' ${frameAncestors}`,
];

const headers = [
const defaultHeaders = [
{
key: 'X-DNS-Prefetch-Control',
value: 'on',
Expand All @@ -39,12 +39,30 @@ const headers = [
];

if (forceSSL) {
headers.push({
defaultHeaders.push({
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload',
});
}

const trackerHeaders = [
{
key: 'Access-Control-Allow-Origin',
value: '*',
},
];

const headers = [
{
source: '/:path*',
headers: defaultHeaders,
},
{
source: '/script.js',
headers: trackerHeaders,
},
];

const rewrites = [];

if (collectApiEndpoint) {
Expand All @@ -54,19 +72,6 @@ if (collectApiEndpoint) {
});
}

if (trackerScriptName) {
const names = trackerScriptName?.split(',').map(name => name.trim());

if (names) {
names.forEach(name => {
rewrites.push({
source: `/${name.replace(/^\/+/, '')}`,
destination: '/tracker.js',
});
});
}
}

const redirects = [
{
source: '/settings',
Expand All @@ -85,6 +90,27 @@ const redirects = [
},
];

// Adding rewrites + headers for all alternative tracker script names.
if (trackerScriptName) {
const names = trackerScriptName?.split(',').map(name => name.trim());

if (names) {
names.forEach(name => {
const normalizedSource = `/${name.replace(/^\/+/, '')}`;

rewrites.push({
source: normalizedSource,
destination: '/script.js',
});

headers.push({
source: normalizedSource,
headers: trackerHeaders,
});
});
}
}

if (cloudMode && cloudUrl) {
redirects.push({
source: '/settings/:path*',
Expand Down Expand Up @@ -153,12 +179,7 @@ const config = {
return config;
},
async headers() {
return [
{
source: '/:path*',
headers,
},
];
return headers;
},
async rewrites() {
return [
Expand Down

0 comments on commit 1f52b49

Please sign in to comment.