网友提问:想问一下你的Notion是怎么利用Cloudflare重定向到 tutulifestyle.com 这个域名的,我查了一些教程,但是那种生成Cloudflare里works的代码网站很多都失效了,所以想请教一下你是怎么做的,谢谢!

我的回答:(一个大神网友写信给我的建议,我照着进行了设置)

1.使用super(https://super.so)的SaaS。简单说,它可以把Notion页面变成一个访问速度比Notion页面更正常的网页,也可以按需加Footer那些稍微定制化,内容是自动同步Notion页面,这样就可以把Notion url变成 XXXXX.super.site,国内网络访问也正常。

2.创建一个Cloudfare Worker将传入的HTTP请求代理到指定的目标URL,我使用的Cloudfare worker代码附在这里,这段代码经过几次修改,大概完成的事情就是访客访问的时候让地址栏保持不变、解决重复重定向的错误、4个小时的缓存等,你可以问ChatGPT这段代码具体作用,如果需要也可以让GPT帮你修改成适用于你的代码。

小缺点:只有一个,网页左下角会显示“Made with super”,毕竟免费我可以接受。

TuTu版代码如下:

addEventListener('fetch', event =\\> {  
    event.respondWith(handleRequest(event.request));  
});  
  
async function handleRequest(request) {  
    const targetURL = '[<https://tutulifestyle.super.site>](<https://tutulifestyle.super.site/>)';  
    const url = new URL(request.url);  
    const newURL = targetURL + url.pathname + url.search;  
  
    const newRequest = new Request(newURL, {  
        method: request.method,  
        headers: request.headers,  
        body: request.method !== 'GET' && request.method !== 'HEAD' ? request.body : null,  
        redirect: 'follow'  
    });  
  
    let response;  
    try {  
        response = await fetch(newRequest);  
    } catch (error) {  
        return new Response('Error fetching the target URL: ' + error.message, { status: 500 });  
    }  
  
    response = new Response(response.body, response);  
    response.headers.set('Access-Control-Allow-Origin', '\\*');  
  
    if (response.status === 200 && request.method === 'GET') {  
        response.headers.set('Cache-Control', 'public, max-age=14400');  
    } else {  
        response.headers.set('Cache-Control', 'no-store');  
    }  
  
    if (request.headers.has('range')) {  
        response.headers.set('Accept-Ranges', 'bytes');  
    }  
  
    return response;  
}