Skip to content

Instantly share code, notes, and snippets.

@utlime
Last active January 21, 2020 12:01
Show Gist options
  • Save utlime/a74f911c73cf0804240b875aeb96f52f to your computer and use it in GitHub Desktop.
Save utlime/a74f911c73cf0804240b875aeb96f52f to your computer and use it in GitHub Desktop.
SP helper
export async function isNginxRouteSPA(route) {
const url = new URL(route, window.location.origin);
url.searchParams.set('_timestamp', Date.now().toString(16));
const response = (await fetch(url.toString(), { "method": "GET", "credentials": "include" }));
return response.headers.get('x-app-type') === 'SPA';
}
export function isCurrentPageSPA() {
return Array.from(document.getElementsByTagName('meta'))
.some(el => el.name === 'version');
}
export async function testNginxRoutesSPA(routes) {
console.table(await Promise.all(routes.map(async (route) => {
return [route, await isNginxRouteSPA(route) ? 'SPA' : 'V3'];
})));
}
export function testCurrentPage() {
console.table([
[window.location.pathname, isCurrentPageSPA() ? 'SPA' : 'V3']
]);
}
function setCookie(name, value, days = 365) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
export function setSPACookie() {
setCookie('spab_spa_headless', 'variation_a');
}
export async function isNginxRouteSPA(route: string) {
const url = new URL(route, window.location.origin);
url.searchParams.set('_timestamp', Date.now().toString(16));
const response = (await fetch(url.toString(), {"method":"GET", "credentials":"include"}));
return response.headers.get('x-app-type') === 'SPA';
}
export function isCurrentPageSPA() {
return Array.from(document.getElementsByTagName('meta'))
.some(el => el.name === 'version');
}
export async function testNginxRoutesSPA(routes: string[]) {
console.table(await Promise.all(routes.map(async (route) => {
return [route, await isNginxRouteSPA(route) ? 'SPA' : 'V3'];
})));
}
export function testCurrentPage() {
console.table([
[window.location.pathname, isCurrentPageSPA() ? 'SPA' : 'V3']
])
}
function setCookie(name: string,value: string,days = 365) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
export function setSPACookie() {
setCookie('spab_spa_headless', 'variation_a')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment