<?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 SRUJAN PJ on Medium]]></title>
        <description><![CDATA[Stories by SRUJAN PJ on Medium]]></description>
        <link>https://medium.com/@srujanpj?source=rss-ad9a41578e71------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*RSqx5NmMZZ7SwWKN</url>
            <title>Stories by SRUJAN PJ on Medium</title>
            <link>https://medium.com/@srujanpj?source=rss-ad9a41578e71------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:57:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@srujanpj/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[The Click That Never Happened]]></title>
            <link>https://medium.com/@srujanpj/the-click-that-never-happened-7e4d015ee91e?source=rss-ad9a41578e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e4d015ee91e</guid>
            <category><![CDATA[header]]></category>
            <category><![CDATA[google-chrome]]></category>
            <category><![CDATA[url]]></category>
            <category><![CDATA[chrome]]></category>
            <category><![CDATA[browser-prerendering]]></category>
            <dc:creator><![CDATA[SRUJAN PJ]]></dc:creator>
            <pubDate>Sat, 03 Oct 2026 15:15:48 GMT</pubDate>
            <atom:updated>2026-10-03T15:15:48.620Z</atom:updated>
            <content:encoded><![CDATA[<p><em>How a “perfectly working” link tracker started double-counting, and the one header that fixed it.</em></p><h3>The use case</h3><p>I was building a small tracking feature: <strong>capture the open count and click count of a link.</strong> Every time someone clicks a tracked link, the server hits an endpoint, bumps a counter, and redirects.</p><pre>http://localhost:3000/click?id=123</pre><p>Simple. Or so I thought.</p><h3>Everything was fine… at first</h3><p><strong>Postman:</strong> one request, one increment.</p><pre>GET http://localhost:3000/click?id=123</pre><pre>→ 200 OK<br>→ click_count: 41 → 42</pre><p><strong>Chrome (early testing):</strong> click the link, count goes up by one.</p><p>I shipped it to testing feeling good.</p><h3>Then the numbers got weird</h3><p>Later, I noticed the counts were <strong>higher than the number of clicks</strong>. I’d click once, and the counter would jump by 2.</p><pre>Expected after 1 click:   click_count = 42<br>Actual in Chrome:         click_count = 43   ❌</pre><p>Server logs told the story:</p><pre>[12:01:03] GET /click?id=123   → count = 42<br>[12:01:03] GET /click?id=123   → count = 43   ← who sent this??</pre><p>Two requests. One click. Postman never did this. Early Chrome never did this. What changed?</p><p>I checked for double event listeners, React strict mode double-invokes, retries, and redirects. None of them were the cause.</p><h3>The culprit: Chrome’s prerender</h3><p>Chrome has a <strong>speculative loading</strong> feature. When it <em>predicts</em> you’re about to visit a page (from the address bar, from links you hover over, or from &lt;script type=&quot;speculationrules&quot;&gt; hints), it can <strong>prerender</strong> that page in the background. That means it fully loads it, including sending the real request to your server, before you&#39;ve even clicked.</p><p>So, from my server’s point of view:</p><ol><li>Chrome prerenders http://localhost:3000/click?id=123 → <strong>request #1</strong> (the ghost)</li><li>The user clicks the link → <strong>request #2</strong> (the real one)</li></ol><p>Both look like normal GET requests. My counter couldn’t tell them apart. Hence the inflated clicks.</p><p>That also explains why it appeared “later.” Chrome’s predictor learns from your behavior, so it only started prerendering after I’d visited that URL a few times.</p><h3>The flow</h3><pre>User hovers / types link<br>        │<br>        ▼<br>Chrome prerenders /click?id=123   ← speculative, user hasn&#39;t clicked yet<br>        │<br>        ▼<br>GET /click?id=123<br>Sec-Purpose: prefetch;prerender<br>        │<br>        ▼<br>Does Sec-Purpose contain &quot;prefetch&quot;/&quot;prerender&quot;?<br>   ├── YES → serve response, DON&#39;T count<br>   └── NO  → real visit → count++</pre><h3>The fix: look at the header</h3><p>Chrome is honest about what it’s doing. Speculative requests carry a header:</p><pre>Sec-Purpose: prefetch;prerender</pre><p>(Plain prefetches send Sec-Purpose: prefetch. Older Chrome versions also used Purpose: prefetch.)</p><p>So the fix is: <strong>if that header is present, don’t count it.</strong></p><h3>References</h3><ul><li>Chrome for Developers: <em>Prerender pages in Chrome for instant page navigations</em> — <a href="https://developer.chrome.com/docs/web-platform/prerender-pages">https://developer.chrome.com/docs/web-platform/prerender-pages</a></li><li>Chrome for Developers: <em>Speculative loading / Speculation Rules</em> — <a href="https://developer.chrome.com/docs/web-platform/implementing-speculation-rules">https://developer.chrome.com/docs/web-platform/implementing-speculation-rules</a></li><li>MDN: <em>Speculation Rules API</em> — <a href="https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API">https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API</a></li><li>MDN: Document.prerendering and the prerenderingchange event — <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/prerendering">https://developer.mozilla.org/en-US/docs/Web/API/Document/prerendering</a></li><li>MDN: Sec-Purpose header — <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Purpose">https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Purpose</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e4d015ee91e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PDF — Internals]]></title>
            <link>https://medium.com/@srujanpj/pdf-internals-b6dde6a012ae?source=rss-ad9a41578e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6dde6a012ae</guid>
            <category><![CDATA[pages]]></category>
            <category><![CDATA[pdf-rendering]]></category>
            <category><![CDATA[xref]]></category>
            <category><![CDATA[fixed-layout]]></category>
            <category><![CDATA[pdf]]></category>
            <dc:creator><![CDATA[SRUJAN PJ]]></dc:creator>
            <pubDate>Thu, 01 Oct 2026 16:12:14 GMT</pubDate>
            <atom:updated>2026-10-01T16:12:14.517Z</atom:updated>
            <content:encoded><![CDATA[<p>PDF is probably one of the most common file formats we use. We open PDFs all the time, but rarely stop to think about how they actually work.</p><p>Initially, I assumed PDFs were basically Word documents — all pages stored the same way, the only real difference being the file extension, .doc vs .pdf. I never bothered to look further.</p><p>A PDF is a <strong>fixed-layout page description</strong>. Unlike HTML, it doesn’t describe content in a way that lets the layout freely reflow based on screen size or available space.</p><h3>What does “fixed layout” mean?</h3><p>In a PDF, the position of content is part of the document itself. For example, a PDF can specify:</p><pre>Put &quot;Invoice&quot; at x=100, y=700<br>Put &quot;Customer: Srujan&quot; at x=100, y=650<br>Draw a line from x=100 to x=500<br>Draw a rectangle at x=100, y=400</pre><p>The PDF is describing how the page should look and where each element goes. That’s why a PDF looks the same whether you open it on a laptop, phone, or another computer.</p><h3>What is reflowable layout?</h3><p>HTML works differently — it describes the <em>structure</em> of content, and the browser decides how to arrange it based on available space:</p><pre>&lt;h1&gt;Invoice&lt;/h1&gt;<br>&lt;p&gt;Customer: Srujan&lt;/p&gt;<br>&lt;p&gt;Amount: ₹10,000&lt;/p&gt;</pre><p>On a wide screen this might appear one way; on a narrow screen the same content can wrap or rearrange. This is called <strong>reflow</strong>.</p><h3>What is markup?</h3><p>Markup adds information to content to describe its structure or meaning:</p><pre>&lt;h1&gt;Hello&lt;/h1&gt;<br>&lt;p&gt;My name is Srujan.&lt;/p&gt;</pre><p>&lt;h1&gt; tells the browser &quot;Hello&quot; is a heading; &lt;p&gt; tells it the rest is a paragraph. HTML is a <em>markup language</em>.</p><p>The difference, in one line each:</p><pre>HTML → &quot;This is a heading. This is a paragraph.&quot;<br>PDF  → &quot;Draw this text here, at this position, using this font and size.&quot;</pre><p>A PDF is much closer to a set of drawing instructions than to structured text.</p><h3>Basics of PDF: Objects</h3><p>Everything inside a PDF — a page, a font, an image, a line of text — is one of a handful of numbered <strong>objects</strong>, written as:</p><pre>N 0 obj<br>&lt;&lt; ...object content... &gt;&gt;<br>endobj</pre><p>N is the object number; the content between obj and endobj is a dictionary, a stream, or both. Objects reference each other by number instead of containing one another directly — a Page object doesn&#39;t <em>hold</em> its text, it <em>points to</em> the object that does:</p><pre>3 0 obj<br>&lt;&lt; /Type /Page /Parent 2 0 R<br>   /Resources &lt;&lt; /Font &lt;&lt; /F1 5 0 R &gt;&gt; &gt;&gt;<br>   /Contents 4 0 R &gt;&gt;<br>endobj</pre><p>4 0 R means &quot;object 4&quot; — the actual content stream. This indirection is what lets a PDF reader load only the handful of objects a given page needs, instead of the whole file.</p><p>A small set of these objects, linked together, is the entire backbone of a PDF: a <strong>Catalog</strong> (the root), pointing to a <strong>Pages</strong> tree, pointing to individual <strong>Page</strong> objects, each pointing to its own <strong>Content Stream</strong> and <strong>Resources</strong>.</p><h3>How a PDF Goes From File to Screen</h3><p>Put together, here’s the full path — from the raw file on disk to pixels in a browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oshaXHG8gFWBCa0wI2BF5w.png" /></figure><ol><li>The <strong>browser</strong> opens or fetches the PDF file.</li><li>A <strong>PDF viewer</strong> (like PDF.js, or the browser’s built-in one) reads the header and jumps straight to the <strong>xref table</strong> — the index mapping each object number to its byte offset in the file.</li><li>The xref table leads to the <strong>Catalog</strong>, the document’s root object.</li><li>The Catalog points to <strong>Pages</strong>, the page tree; the page tree’s /Kids array leads to a specific <strong>Page</strong>.</li><li>That Page object points to two things: its <strong>Content Stream</strong> (the drawing instructions — “put this text here, draw this line there”) and its <strong>Resources</strong> (the fonts, images, and color spaces the content stream refers to).</li><li>The <strong>render engine</strong> walks the content stream operator by operator, pulling in whatever it needs from Resources, and paints the result onto the &lt;canvas&gt; you actually see.</li></ol><p>Every step is just following a reference to the next numbered object — which is why a viewer can jump straight to page 400 of a long PDF without ever reading pages 1 through 399.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6dde6a012ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is RAG? — Beginner]]></title>
            <link>https://medium.com/@srujanpj/what-is-rag-beginner-68fd94c0f6a7?source=rss-ad9a41578e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/68fd94c0f6a7</guid>
            <category><![CDATA[rag-model]]></category>
            <dc:creator><![CDATA[SRUJAN PJ]]></dc:creator>
            <pubDate>Sun, 08 Jun 2025 12:32:38 GMT</pubDate>
            <atom:updated>2025-06-08T12:32:38.773Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is RAG? — Beginner</h3><p>In this blog, I’m sharing what I learned with the help of AI (ChatGPT) while building a simple chat app. The app pulls data from a MySQL table, trains on it, and then uses that info to answer questions. Just a heads-up — this example is mostly for learning, so the approach might not be the most efficient, but it’s definitely helped me get a better grasp, even if just a little bit!</p><h3><strong>Overview</strong></h3><p>RAG stands for <strong>Retrieval-Augmented Generation</strong>, which combines two main capabilities:</p><p><strong>Retrieval</strong>: In this example, we fetch stored data from a MySQL database using a custom function get_knowledgebase.</p><p><strong>Generation</strong>: We use language models like GPT2Tokenizer and GPT2LMHeadModel to generate natural language responses based on the retrieved information.</p><h3><strong>Architecture</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*yAljrikFSfGRaRRJGesS_w.png" /><figcaption>Architecture of the Chat App</figcaption></figure><p>The application retrieves data from a MySQL database, generates embeddings for the text using SentenceTransformer, and stores those embeddings in a FAISS vector index. When a user asks a question, the query is also converted into an embedding and searched against the stored embeddings to find the most relevant documents. These documents are then used as context, and GPT generates a final answer based on that context and the original question.</p><h3>Code References</h3><blockquote>Getting Data from MySql</blockquote><pre>   db_config = {<br>        &#39;host&#39;: &#39;&lt;host&gt;&#39;,<br>        &#39;user&#39;: &#39;&lt;user&gt;&#39;,<br>        &#39;password&#39;: &#39;&lt;password&gt;&#39;,<br>        &#39;database&#39;: &#39;chat&#39;<br>    }<br><br>    connection = mysql.connector.connect(**db_config)<br>    cursor = connection.cursor()<br>    cursor.execute(&quot;SELECT topic, description FROM knowledgebase&quot;)<br>    data = cursor.fetchall()<br>    cursor.close()<br>    connection.close()<br>    return data</pre><blockquote>Loading Data and Creating embedding</blockquote><pre> # Fetch raw data from database<br>   data = get_knowledgebase()<br>   self.documents = [desc for _, desc in data]<br>   self.topics = [topic for topic, _ in data]<br><br> # Load embedding model<br>   self.embedder = SentenceTransformer(&#39;all-MiniLM-L6-v2&#39;)<br><br> # Create embeddings for documents<br>   self.doc_embeddings = self.embedder.encode(self.documents, convert_to_tensor=True).cpu().detach().numpy()</pre><blockquote>Building FAISS Index</blockquote><pre>  # Build FAISS index<br>    dim = self.doc_embeddings.shape[1]<br>    self.index = faiss.IndexFlatL2(dim)<br>    self.index.add(self.doc_embeddings)</pre><blockquote>Load GPT-Model</blockquote><pre>   self.tokenizer = GPT2Tokenizer.from_pretrained(&#39;gpt2&#39;)<br>   self.model = GPT2LMHeadModel.from_pretrained(&#39;gpt2&#39;)<br>   self.model.eval()</pre><blockquote>Generate Answer</blockquote><pre>    def retrieve(self, query, top_k=3):<br>        query_vec = self.embedder.encode([query], convert_to_tensor=True).cpu().detach().numpy()<br>        D, I = self.index.search(query_vec, top_k)<br>        return [self.documents[i] for i in I[0]]<br><br>    def generate_answer(self, query):<br>        retrieved_docs = self.retrieve(query)<br>        context = &quot; &quot;.join(retrieved_docs)<br>        input_text = f&quot;Context: {context}\nQuestion: {query}\nAnswer:&quot;<br>        inputs = self.tokenizer.encode(input_text, return_tensors=&quot;pt&quot;)<br><br>        outputs = self.model.generate(inputs, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2)<br>        <br>        # Decode the full output<br>        full_output = self.tokenizer.decode(outputs[0], skip_special_tokens=True)<br><br>        # Extract only the answer part<br>        answer = full_output.split(&quot;Answer:&quot;)[-1].strip();<br>        <br>        return answer</pre><h3>Output</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fJOt619RtquA3puxiIJtvw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68fd94c0f6a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>