<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by awayyao on Medium]]></title>
        <description><![CDATA[Stories by awayyao on Medium]]></description>
        <link>https://medium.com/@awayyao?source=rss-12af7c71e270------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*xtmKCvzuqC_IokJh.jpg</url>
            <title>Stories by awayyao on Medium</title>
            <link>https://medium.com/@awayyao?source=rss-12af7c71e270------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 00:06:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@awayyao/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Keep page state between page navigation in Nextjs 13 (using app router schema)]]></title>
            <link>https://medium.com/@awayyao/keep-page-state-between-page-navigation-in-nextjs-13-using-app-router-schema-f6eacd0300ad?source=rss-12af7c71e270------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6eacd0300ad</guid>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[awayyao]]></dc:creator>
            <pubDate>Fri, 15 Dec 2023 03:12:19 GMT</pubDate>
            <atom:updated>2023-12-15T12:31:33.485Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gfTPPyUrBkGCFHU7kYWYVw.jpeg" /></figure><h3>Problem</h3><p>Next’s 13 introduces a new route patten: app router. When we develop client component page, its internal state always lose when navigate to another page. So how to “keep alive” history page? So when we go back to history page, the content is still same as we leave it?</p><h3>Solution</h3><p>In general, there’re 3 ways to do that.</p><ul><li>Convert to server component</li><li>Use url to keep state</li><li>Use global cache to keep state</li></ul><h3>Convert to server component</h3><p>That’s easiest way to let nextjs handle state. Nextjs will keep content and scroll positioin as browser does. (Keep mind that nextjs does not use browser bfcache in first place)</p><p>But in some situation, we can’t do that. Go on reading.</p><h3>Use url to keep state</h3><p>Remember nextjs always rerender client component when router changes, so we can use url to keep simple state. So delete useState and use Link and query parameter to keep state. And use replace = true in Link so it will not push new page to browser history.</p><p>Here is a counter example.</p><pre>&quot;use client&quot;;<br>import Link from &quot;next/link&quot;;<br>import React, { useState } from &quot;react&quot;;<br>import { useSearchParams } from &quot;next/navigation&quot;;<br>​<br>const CounterPage: React.FC = () =&gt; {<br>  const searchParams = useSearchParams();<br>​<br>  const n = parseInt(searchParams.get(&quot;n&quot;) || &quot;0&quot;);<br>​<br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Counter Page&lt;/h1&gt;<br>      &lt;p&gt;Count: {n}&lt;/p&gt;<br>      &lt;div&gt;<br>        &lt;Link replace href={`/count?n=${n + 1}`} scroll={false}&gt;<br>          &lt;button&gt;Increment&lt;/button&gt;<br>        &lt;/Link&gt;<br>        &lt;Link replace href={`/count?n=${n - 1}`} scroll={false}&gt;<br>          &lt;button&gt;Decrement&lt;/button&gt;<br>        &lt;/Link&gt;<br>      &lt;/div&gt;<br>      &lt;div&gt;<br>        &lt;div&gt;<br>          &lt;Link href=&quot;/about&quot;&gt;About&lt;/Link&gt;<br>        &lt;/div&gt;<br>      &lt;/div&gt;<br>    &lt;/div&gt;<br>  );<br>};<br>​<br>export default CounterPage;<br>​</pre><h3>Use global cache to keep state</h3><p>If your state is too complex to fit into url parameter, you can store it in global cache. There’re several choice for that: localstorage, context provider.</p><p>Now you can save your page state whenever umount page. Something like that:</p><pre>name: {<br>      pageProps,<br>      scrollPos: 0,<br>      name,<br>}</pre><p>By now, <a href="https://github.com/AlexSapoznikov/react-next-keep-alive">react-next-keep-alive</a> only support page router style, you can implement new one by yourself. If there’s any open source project supporting app router, pleasce comment to let me know, thanks.</p><h3>Future</h3><p>We’re looking forward that nextjs new version can handle that common requirement for developers. Vue3 support keep alive tag so framework handle all the things.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6eacd0300ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prevent DDOS with fail2ban]]></title>
            <link>https://medium.com/@awayyao/prevent-ddos-with-fail2ban-37f2a6dd6380?source=rss-12af7c71e270------2</link>
            <guid isPermaLink="false">https://medium.com/p/37f2a6dd6380</guid>
            <category><![CDATA[ddos]]></category>
            <category><![CDATA[fail2ban]]></category>
            <dc:creator><![CDATA[awayyao]]></dc:creator>
            <pubDate>Thu, 20 Jun 2019 00:04:57 GMT</pubDate>
            <atom:updated>2019-06-20T00:04:57.780Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, web server receive many requests from different IP addresses. Every request has same url and user-agent header, but source IP is different. The request flood is so high and out bandwidth is almost full.</p><p>fail2ban is a software to solve this problem. Although at first, it is used to prevent one IP try many password to login linux.</p><h3>Install</h3><p>Install most recent version for fail2ban. version 0.9 and below has some performance problem, when ddos flood is high, fail2ban will halt. I install version 0.11(June, 2019 when write this article).</p><p>Notice: If you upgrade instead of install, make sure remove old one before install new one. Otherwise, fail2ban python lib will still there and make fail2ban fail to run.</p><pre># wget <a href="https://github.com/fail2ban/fail2ban/archive/0.11.zip">https://github.com/fail2ban/fail2ban/archive/0.11.zip</a><br># unzip 0.11.zip<br># cd fail2ban-0.11<br># sudo python setup.py install</pre><h3>Configure</h3><p>fail2ban first fetch data from log file, use filter to find bad ips, then use your rule to check if ban it or not. Then ban it using iptables command.</p><p>config file path is under /etc/fail2ban</p><h3>1. create filter</h3><p>filter file is under /etc/fail2ban/filter.d/, we create new filter my-filter.conf</p><pre><strong>[Definition]</strong><br>​<br>failregex = ^&lt;HOST&gt; .* &quot;(GET|POST|HEAD) /ddos_url 400<br>​<br>ignoreregex =</pre><p>you can make small log file to test regex</p><pre># sudo fail2ban-regex /tmp/test.log /etc/fail2ban/filter.d/my_filter.conf -v<br>​</pre><h3>2. configure jail</h3><p>add configure section in /etc/fail2ban/jail.conf</p><pre>[nginx-ddos]<br>​<br>enabled = true<br>port    = https<br>filter  = my_filter<br>logpath = /var/log/nginx/access.log<br>maxretry = 1<br>bantime = 86400<br>findtime = 600</pre><p>We create a jail named nginx-ddos, which uses my_filter to find bad ips, log file is logpath, if ip request match regex in 600 seconds for 1 time, it will be baned next 86400 seconds(one day). You can adjust bantime, findtime, maxretry to fit your requirement.</p><h3>Mange Fail2ban</h3><p>Instead of restart, you can always reload fail2ban to make configuration work.</p><pre># sudo fail2ban-client reload nginx-ddos</pre><p>You can check if it works by check status</p><pre># sudo fail2ban-client status nginx-ddos</pre><p>and also check fail2ban logs for details</p><pre># sudo tail -f /var/log/fail2ban.log</pre><p>the log shows</p><pre>2019-06-20 07:51:02,053 fail2ban.filter         [30202]: INFO    [nginx-doseeing] Found x.x.x.x - 2019-06-20 07:51:01<br>2019-06-20 07:51:02,406 fail2ban.actions        [30202]: NOTICE  [nginx-doseeing] Ban x.x.x.x</pre><h3>TIPS</h3><ol><li>fail2ban has performance issue before v0.10, we recommend to use v0.10+.</li><li>fail2ban startup takes several seconds to read and you will not see any change in log. So do not use restart often, use reload.</li><li>If hacker use every ip for one time, fail2ban is not able to solve that situation. Consider block ip section instead of single ip. But it may ban normal IP by mistake.</li><li>If you use HTTPS service in nginx, the overhead for request is high, so when hacker send 1k, your server will response with 10k data(certification and related information? I guess). This problem needs more research in the future.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37f2a6dd6380" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue.js解决了什么问题]]></title>
            <link>https://medium.com/@awayyao/vue-js%E8%A7%A3%E5%86%B3%E4%BA%86%E4%BB%80%E4%B9%88%E9%97%AE%E9%A2%98-ad7fd7fde9f4?source=rss-12af7c71e270------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad7fd7fde9f4</guid>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[awayyao]]></dc:creator>
            <pubDate>Fri, 19 Apr 2019 02:08:07 GMT</pubDate>
            <atom:updated>2019-04-19T02:08:07.740Z</atom:updated>
            <content:encoded><![CDATA[<p>传统网页是html+css的集合，主要功能是展示。大部分交互用超链接就能完成。比如新闻类，博客类的网站。遇到逻辑判断通常在服务完成，比如注册信息是否符合要求，密码是否正确等等。</p><p>然而后来产品经理发现一个规律，用户交互频率和用户留存率之间有一些关系。如果给用户一个按钮，还能马上给出反馈，那有一小部分用户就会每天来点这个按钮。但如果用户点了按钮，发现没有任何效果，用户就会离开。于是，产品经理需要能更快响应用户点击的方法。传统网页每次操作需要刷新网页显然不能更快了。于是javascript出现了，它能在本地就处理一些简单的用户交互逻辑，比如点了按钮马上给你一个loading图标。这样，即使后台还是很慢，但是好歹用户还没有走。</p><p>产品经理很满意，于是决定加大力度。因为他们发现有时候自己的交互想法用html的官方控件根本不够用。比如有时候需要一个能够选择时间段的日期选择控件，有时候需要一个能够插入图片的编辑器。这时候，javascript变的越来越复杂，于是出现了jQuery这样框架，它封装了一系列javascipt的原生API，让js操作html的dom对象变的更加简单。</p><p>但随着业务功能的越来越复杂，jQuery也不够用了。因为网页越来越复杂，用控件级别的jQuery确实解决了交互插件的开发功能，但是网站又出现了控件状态同步的问题。</p><p>如果你做了一个音乐网站，有个在线的播放器，那么你希望歌曲播放的时候有播出的时间，还有一个进度条可以支持拖动位置来播放不同的时间。这两个控件其实都需要现在播放时间，所以一旦改了这个时间就需要更新这两个控件的状态，如果一个不小心忘记写了某个地方，就会出现控件状态和数据不同步的情况。</p><p>于是vue.js就出现了。它支持和控件的状态和数据绑定在一起。以后这些更新控件只需要更新数据源就可以了，这节省了不少手写更新控件代码的时间。而且vue.js不止步于控件状态的更新，对html的所有内容，不管是html代码内容，dom的属性，甚至html的结构都能做更新。这让网页这种页面的概念变的模糊了，网页进入了应用时代。这种把数据和控件绑定的做法就是一种设计模式，成为MVVM。Model是数据，View是控件（也可以是网页），ViewModel就是Vue做的事情，把数据和控件绑定在一起。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad7fd7fde9f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>