Open CarlosOtto opened 4 years ago
Do you have a DNS-Record with your domain configured on Cloudflare? You need to have a valid record for all this to work. Doesn't really matter where it points to.
Thks for your quick response. I would say so.
[image: image.png]
On Wed, May 13, 2020 at 12:11 PM Felix Gerberding notifications@github.com wrote:
Do you have a DNS-Record with your domain configured on Cloudflare? You need to have a valid record for all this to work. Doesn't really matter where it points to.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/stephenou/fruitionsite/issues/10#issuecomment-627887510, or unsubscribe https://github.com/notifications/unsubscribe-auth/APRZHHCQZ7TJ2ZXAZ2IITMLRRJW3NANCNFSM4M7ASK4Q .
-- Carlos de Otto
After doing all setup, I have not managed to make it work. Have no idea why. DNS_PROBE_FINISHED_NXDOMAIN is all I can share with you.
And the script of course: / CONFIGURATION STARTS HERE /
/ Step 1: enter your domain name like fruitionsite.com / const MY_DOMAIN = 'senda.works';
/*
The value on the right is the Notion page ID */ const SLUG_TO_PAGE = { '': '101ce05cea1c41f08eaf99b93ad995e3', };
/ Step 3: enter your page title and description for SEO purposes / const PAGE_TITLE = 'Senda'; const PAGE_DESCRIPTION = 'El alquiler sin sobresaltos.';
/ Step 4: enter a Google Font name, you can choose from https://fonts.google.com / const GOOGLE_FONT = 'Open Sans';
/ Step 5: enter any custom scripts you'd like / const CUSTOM_SCRIPT = ``;
/ CONFIGURATION ENDS HERE /
const PAGE_TO_SLUG = {}; const slugs = []; const pages = []; Object.keys(SLUG_TO_PAGE).forEach(slug => { const page = SLUG_TO_PAGE[slug]; slugs.push(slug); pages.push(page); PAGE_TO_SLUG[page] = slug; });
addEventListener('fetch', event => { event.respondWith(fetchAndApply(event.request)); });
const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, HEAD, POST, PUT, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', };
function handleOptions(request) { if (request.headers.get('Origin') !== null && request.headers.get('Access-Control-Request-Method') !== null && request.headers.get('Access-Control-Request-Headers') !== null) { // Handle CORS pre-flight request. return new Response(null, { headers: corsHeaders }); } else { // Handle standard OPTIONS request. return new Response(null, { headers: { 'Allow': 'GET, HEAD, POST, PUT, OPTIONS', } }); } }
async function fetchAndApply(request) { if (request.method === 'OPTIONS') { return handleOptions(request); } let url = new URL(request.url); const notionUrl = 'https://www.notion.so' + url.pathname; let response; if (url.pathname.startsWith('/app') && url.pathname.endsWith('js')) { response = await fetch(notionUrl); let body = await response.text(); response = new Response(body.replace(/www.notion.so/g, MY_DOMAIN).replace(/notion.so/g, MY_DOMAIN), response); response.headers.set('Content-Type', 'application/x-javascript'); } else if ((url.pathname.startsWith('/api'))) { // Forward API response = await fetch(notionUrl, { body: request.body, headers: { 'content-type': 'application/json;charset=UTF-8', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36' }, method: 'POST', }); response = new Response(response.body, response); response.headers.set('Access-Control-Allow-Origin', '*'); } else if (slugs.indexOf(url.pathname.slice(1)) > -1) { const pageId = SLUG_TO_PAGE[url.pathname.slice(1)]; return Response.redirect('https://' + MY_DOMAIN + '/' + pageId, 301); } else { response = await fetch(notionUrl, { body: request.body, headers: request.headers, method: request.method, }); response = new Response(response.body, response); response.headers.delete('Content-Security-Policy'); response.headers.delete('X-Content-Security-Policy'); }
return appendJavascript(response, SLUG_TO_PAGE); }
class MetaRewriter { element(element) { if (PAGE_TITLE !== '') { if (element.getAttribute('property') === 'og:title' || element.getAttribute('name') === 'twitter:title') { element.setAttribute('content', PAGE_TITLE); } if (element.tagName === 'title') { element.setInnerContent(PAGE_TITLE); } } if (PAGE_DESCRIPTION !== '') { if (element.getAttribute('name') === 'description' || element.getAttribute('property') === 'og:description' || element.getAttribute('name') === 'twitter:description') { element.setAttribute('content', PAGE_DESCRIPTION); } } if (element.getAttribute('property') === 'og:url' || element.getAttribute('name') === 'twitter:url') { element.setAttribute('content', MY_DOMAIN); } if (element.getAttribute('name') === 'apple-itunes-app') { element.remove(); } } }
class HeadRewriter { element(element) { if (GOOGLE_FONT !== '') { element.append(`
`, { html: true }); } element.append(``, { html: true }) } } class BodyRewriter { constructor(SLUG_TO_PAGE) { this.SLUG_TO_PAGE = SLUG_TO_PAGE; } element(element) { element.append(` ${CUSTOM_SCRIPT}`, { html: true }); } } async function appendJavascript(res, SLUG_TO_PAGE) { return new HTMLRewriter() .on('title', new MetaRewriter()) .on('meta', new MetaRewriter()) .on('head', new HeadRewriter()) .on('body', new BodyRewriter(SLUG_TO_PAGE)) .transform(res); }