<?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 Pradip Bhusnar on Medium]]></title>
        <description><![CDATA[Stories by Pradip Bhusnar on Medium]]></description>
        <link>https://medium.com/@PradipBhusnar?source=rss-70cbb7a28ff2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*z4zaoyZ7Vtl_moyz</url>
            <title>Stories by Pradip Bhusnar on Medium</title>
            <link>https://medium.com/@PradipBhusnar?source=rss-70cbb7a28ff2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 14:07:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@PradipBhusnar/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[How to implement coding standards in your organization]]></title>
            <link>https://medium.com/@PradipBhusnar/how-to-implement-coding-standards-in-your-organization-89e3df68a84f?source=rss-70cbb7a28ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/89e3df68a84f</guid>
            <category><![CDATA[code-review]]></category>
            <category><![CDATA[guidelines]]></category>
            <category><![CDATA[standards]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Pradip Bhusnar]]></dc:creator>
            <pubDate>Wed, 07 Aug 2024 17:10:17 GMT</pubDate>
            <atom:updated>2024-08-08T06:04:24.399Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="How to implement coding standards in your organization" src="https://cdn-images-1.medium.com/max/1024/1*_ZJ-DKOU2StwpfitlGIbCA.png" /></figure><p>Implementing coding standards in your organization is essential for maintaining consistent, readable, and high-quality code. These guidelines help developers write code that is easier to understand, maintain, and collaborate on. Here’s how you can implement coding standards in your organization effectively.</p><h3>Why Are Coding Standards Important?</h3><p>Before diving into implementation, let’s explore why coding standards are crucial:</p><ul><li><strong>Better Code Quality</strong>: Adhering to coding standards helps developers write secure, efficient, and maintainable code, reducing errors and improving overall performance.</li><li><strong>Consistency Across Projects</strong>: Coding standards ensure that code is written consistently, making it easier for developers to switch between tasks and collaborate.</li><li><strong>Improved Readability and Maintainability</strong>: Consistent code is easier to read, understand, and modify, saving time and reducing errors.</li><li><strong>Faster Development</strong>: Developers can write code more quickly and with fewer errors by following a set of guidelines.</li><li><strong>Better Scalability</strong>: Coding standards make it easier to add new features and maintain the code as the codebase grows.</li><li><strong>Higher Collaboration and Communication</strong>: Following the same guidelines makes it easier for developers to understand and modify each other’s code.</li><li><strong>Compliance</strong>: Coding standards help ensure your code meets regulatory requirements and certifications. (e.g., PCI DSS, ISO/IEC 27001).</li></ul><h3>Establishing Coding Standards</h3><p>The first step is to agree and document the guidelines the team should follow. A coding standards document outlines the conventions, rules, and guidelines for writing code within a development team. It typically includes rules on coding style, documentation, error handling, etc. Creating this document should involve the entire development team to ensure consensus.</p><h4>Criteria for Task Completion</h4><p>Defining completion criteria helps ensure the software meets the required standards and is ready for deployment. Here are the typical criteria:</p><ul><li><strong>Testing</strong>: The implementer must perform thorough tests, including unit, integration, and system tests, to ensure the code is error-free.</li><li><strong>Acceptance Criteria</strong>: The code should meet the specified requirements and produce the expected results as defined in the acceptance criteria for each user story.</li><li><strong>Code Review</strong>: Each user story should undergo a peer review to catch issues, ensure adherence to standards, and share knowledge.</li><li><strong>Traceability</strong>: Every deliverable should be associated with its corresponding user story for better traceability.</li><li><strong>Task Closure</strong>: All tasks linked to the user story should be marked as closed.</li><li><strong>Comments and Naming</strong>: The code should include comments, and variable names should be clear and understandable.</li><li><strong>Stakeholder Communication</strong>: Business-related document deliverables should be communicated to stakeholders.</li><li><strong>Pull Requests</strong>: A pull request should be linked to the user story to ensure transparency and traceability.</li></ul><h4>Coding Guidelines</h4><p>Coding guidelines define how source code should be written and formatted to maintain consistency and readability.</p><ol><li><strong>Programming Language Guidelines</strong>: <br>Select coding standards that fit your programming language, software type, and team size. Here are some popular standards to consider:</li></ol><ul><li><strong>JavaScript</strong>: <a href="https://google.github.io/styleguide/jsguide.html">JavaScript Style Guides</a></li><li><strong>Typescript</strong>: <a href="https://google.github.io/styleguide/tsguide.html">Typescript Style Guides</a></li><li><strong>Java: </strong><a href="https://google.github.io/styleguide/javaguide.html">Java Style Guides</a></li><li><strong>React/jsx</strong>: <a href="https://airbnb.io/javascript/react/">React/jsx Style Guides</a></li><li><strong>Next.js:</strong> <a href="https://joshtronic.com/2024/03/17/airbnb-style-guide-nextjs/">Next.js Style Guides</a></li><li><strong>Java Spring-Boot</strong>: Java Spring-Boot Style Guides</li></ul><p><strong>2. Code Formatting</strong>: Indentation, line length.</p><ul><li>Linter Use: Integrate a linter with your IDE to catch errors and enforce coding standards in real time.</li><li>Prettier</li><li>Stylelint</li></ul><p><strong>3</strong>. <strong>Naming Conventions</strong>: Variables, classes, functions.<br>Use descriptive, variables and function names.</p><ul><li><strong>Docstrings</strong>: complex logic functions should have docstrings describing inputs, and outputs, and a brief function description.</li><li><strong>Variable Types</strong>: Include variable types in function definitions for clarity.</li></ul><p>4. <strong>Code Organization</strong>: File structure, commenting.</p><p><strong>5.</strong> <strong>Best Practices</strong>: Error handling, security.</p><p><strong>6. Branching</strong>: New implementations or bug fixes should be done on separate branches named according to a defined convention (e.g., “feature/&lt;user_story_id&gt;&lt;descriptive_name&gt;”).</p><h4>Review Guidelines</h4><p>Regular code reviews ensure adherence to coding standards and provide opportunities for knowledge sharing.</p><ul><li><strong>Independent Reviews</strong>: Reviews should be conducted by someone other than the implementer for an unbiased perspective.</li><li><strong>Isolated Reviews</strong>: Reviews should be done in isolation from the implementer to focus solely on the code.</li><li><strong>Implementer Tagging</strong>: The implementer should add a tag indicating their name for accountability.</li><li><strong>Highlighting Issues</strong>: Reviewers should highlight and request corrections for any identified “bad code” according to the coding guidelines.</li></ul><h4>Communicate and Train Your Team</h4><p>Ensure all developers understand and commit to the standards. Provide resources like guidelines, best practices, and code examples. Training can include:</p><ul><li><strong>Clear Guidelines</strong>: Detailed documents with examples.</li><li><strong>Hands-On Exercises</strong>: Workshops and online courses.</li><li><strong>Feedback and Support</strong>: Continuous guidance during the learning phase.</li></ul><h4>Use Automated Tools</h4><p>Enforcing standards can be challenging. Automated tools like <strong>Codacy Quality</strong> <strong>/</strong> <strong>Sonar-qube</strong> bots can help:</p><ul><li><strong>Customize Rule Sets</strong>: Align with your coding standards.</li><li><strong>Quality Thresholds</strong>: Ensure consistent code quality across teams and projects.</li><li><strong>Automated Reviews</strong>: Save time and focus on core development tasks.</li></ul><h4>Monitor Adherence</h4><p>Regularly monitor adherence through:</p><ul><li><strong>Code Reviews</strong>: Peer reviews for consistency.</li><li><strong>Automated Tests</strong>: Tools to catch deviations early.</li></ul><p>Address issues promptly, whether they stem from a need for further training or adjustments to the standards.</p><h4>Continuously Review and Improve</h4><p>Coding standards should evolve with your organization. Regularly review and update them:</p><ul><li><strong>New Projects</strong>: Tailor standards to project requirements.</li><li><strong>Significant Changes</strong>: Adjust for major project updates.</li><li><strong>Emerging Practices</strong>: Incorporate new technologies and best practices.</li></ul><p>Involve the team in these updates to ensure everyone agrees and understands the changes.</p><h3>Conclusion</h3><p>Coding standards are vital for high-quality software development. They improve code readability, maintainability, and consistency. Tools like <strong>Codacy Quality</strong> simplify the implementation and adherence to these standards, making your development process more efficient and effective.</p><p>By mastering coding standards and best practices, development teams can build robust, efficient software that meets current requirements and stands the test of time. Coding best practices should be regularly reviewed and updated to incorporate new learnings and technological advancements. This continuous improvement ensures high code quality and an effective development process.</p><p>By following these steps, your organization can build better software, faster, and with fewer errors</p><p><em>Thanks for reading! If you enjoyed reading this article, please </em><strong><em>click the 👏 button and share to help others find it!</em></strong><em> Feel free to leave a comment 💬 below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89e3df68a84f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Resilient Offline-First ReactJS CRUD Apps with Redux Toolkit and Redux-Offline or Service…]]></title>
            <link>https://medium.com/@PradipBhusnar/building-resilient-offline-first-reactjs-crud-apps-with-redux-toolkit-and-redux-offline-or-service-e14d5f053d92?source=rss-70cbb7a28ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e14d5f053d92</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[offline-first]]></category>
            <category><![CDATA[pwa]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[service-worker]]></category>
            <dc:creator><![CDATA[Pradip Bhusnar]]></dc:creator>
            <pubDate>Thu, 11 Jul 2024 14:35:51 GMT</pubDate>
            <atom:updated>2024-07-11T14:35:51.369Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building Resilient Offline-First ReactJS CRUD Apps with Redux Toolkit and Redux-Offline or Service Worker: A Comprehensive Guide</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rkgTG_IFlj4b5BJvyHd1Cg.png" /><figcaption>Offline-First ReactJS CRUD Apps with Redux Toolkit and Redux-Offline or Service Worker</figcaption></figure><p>This blog post dives into building a ReactJS application that can handle CRUD (Create, Read, Update, Delete) operations seamlessly, even when the user encounters network issues. We’ll achieve this using two methods:</p><p>1. Redux Toolkit (RTK) and redux-offline</p><p>2. Redux Toolkit (RTK) and service worker</p><h3>Why Offline-First?</h3><p>Offline-first applications improve user experience by ensuring that the app remains functional even without an internet connection. This is particularly important for users in areas with unreliable internet access. By storing data locally and synchronizing it with the server when the connection is restored, we can provide a smooth and uninterrupted experience.</p><p><strong>The Challenge: Network Outages and Data Loss</strong></p><p>Traditional CRUD applications can falter when the internet connection drops. Data might not be saved or retrieved immediately, leading to a frustrating user experience. Our goal is to design a solution that tackles this challenge by enabling:</p><ul><li><strong>Offline Operations:</strong> Allow users to interact with the application for CRUD actions despite network issues.</li><li><strong>Data Persistence:</strong> Store data locally until a network connection is re-established, ensuring data integrity.</li></ul><h3>Tools and Libraries</h3><p>To build our offline-first CRUD app, we’ll use the following technologies:</p><ul><li><strong>ReactJS</strong>: A JavaScript library for building user interfaces.</li><li><strong>Redux Toolkit (RTK)</strong>: A set of tools and best practices for efficient Redux development.</li><li><strong>IndexedDB</strong>: A low-level API for client-side storage of significant amounts of structured data.</li><li><strong>Redux-offline</strong>: Enables offline functionality within the Redux ecosystem.</li><li><strong>Workbox</strong>: A set of libraries for Progressive Web Apps (PWAs) to handle caching and offline capabilities.</li></ul><h4>Setting Up the Project</h4><p>First, let’s create a new React application and add the necessary dependencies.</p><pre>npm install @reduxjs/toolkit react-redux</pre><h3><strong>1. Embracing Offline-First with Redux Toolkit and </strong><strong>redux-offline</strong></h3><p>Here’s a breakdown of key approaches to achieve offline support:</p><ol><li><strong>Optimistic UI Updates:</strong> Upon a CRUD action, update the UI locally to reflect the change <strong>immediately</strong>. This enhances the user experience as they see the operation reflected without waiting for network confirmation.</li><li><strong>Data Caching:</strong> Store the CRUD operation data locally in a persistent storage mechanism like IndexedDB using redux-offline. This ensures data is not lost even when the network is down.</li><li><strong>Background Synchronization:</strong> Once the network connection resumes, the application attempts to synchronize the cached data with the server, ensuring data consistency across devices.</li></ol><h4><strong>Implementation with Redux Toolkit (RTK) and </strong>redux-offline</h4><p><strong>1. Setting Up:</strong></p><p>Start by installing the necessary libraries:</p><pre>npm install redux-offline localforage axios</pre><p>Use code <a href="https://medium.com/faq#coding">with caution.</a></p><ul><li>redux-offline: Enables offline functionality within the Redux ecosystem.</li><li>redux-thunk: Middleware for handling asynchronous actions in Redux.</li><li>localforage: Provides a convenient interface for interacting with IndexedDB.</li><li>@reduxjs/toolkit: Offers simplified Redux store setup and action creation.</li></ul><p><strong>2. Redux Store with RTK:</strong></p><p>Create a Redux store using the Redux Toolkit. We’ll start by creating a features folder to house our slices and reducers.</p><pre>// src/app/store/index.js<br><br>import { configureStore } from &#39;@reduxjs/toolkit&#39;;<br>import rootReducer from &#39;./reducers&#39;; // Replace with your actual reducers<br>import { offline } from &#39;redux-offline&#39;;<br>import offlineConfig from &#39;redux-offline/lib/defaults&#39;;<br><br>offlineConfig.persistOptions = { storage: localforage }; // Configure IndexedDB storage<br>const store = configureStore({<br>  reducer: rootReducer,<br>  enhancers: [offline(offlineConfig)], // Enable offline functionality<br>});<br>export default store;</pre><p>This code defines the store with the combined reducers and configures offline support using redux-offline, and specifies IndexedDB as the storage option.</p><p><strong>3. Redux Actions with RTK (Improved Example):</strong></p><p>Configure redux-offline with default settings. You can customize it further based on your needs.</p><pre>// src/app/store/features/itemsSlice.js<br><br>import { createSlice, createAsyncThunk } from &#39;@reduxjs/toolkit&#39;;<br>import axios from &#39;axios&#39;;<br><br>const initialState = {<br>  items: [],<br>  status: &#39;idle&#39;,<br>  error: null,<br>};<br><br>export const fetchItems = createAsyncThunk(&#39;items/fetchItems&#39;, async () =&gt; {<br>  const response = await axios.get(&#39;/api/items&#39;);<br>  return response.data;<br>});<br><br>export const addItem = createAsyncThunk(&#39;items/addItem&#39;, async (item) =&gt; {<br>  const response = await axios.post(&#39;/api/items&#39;, item);<br>  return response.data;<br>});<br><br>const itemsSlice = createSlice({<br>  name: &#39;items&#39;,<br>  initialState,<br>  reducers: {<br>    itemAdded: (state, action) =&gt; {<br>      state.items.push(action.payload);<br>    },<br>  },<br>  extraReducers: (builder) =&gt; {<br>    builder<br>      .addCase(fetchItems.pending, (state) =&gt; {<br>        state.status = &#39;loading&#39;;<br>      })<br>      .addCase(fetchItems.fulfilled, (state, action) =&gt; {<br>        state.status = &#39;succeeded&#39;;<br>        state.items = action.payload;<br>      })<br>      .addCase(fetchItems.rejected, (state, action) =&gt; {<br>        state.status = &#39;failed&#39;;<br>        state.error = action.error.message;<br>      })<br>      .addCase(addItem.fulfilled, (state, action) =&gt; {<br>        state.items.push(action.payload);<br>      });<br>  },<br>});<br><br>export default itemsSlice.reducer;</pre><p><strong>4. Creating CRUD Operations</strong></p><p>Next, let’s create a simple component to perform CRUD operations. We’ll use the Redux hooks useDispatch and useSelector to interact with our store.</p><pre>//src/components/ItemList.js<br><br>import React, { useEffect, useState } from &#39;react&#39;;<br>import { useDispatch, useSelector } from &#39;react-redux&#39;;<br>import { fetchItems, addItem } from &#39;../features/itemsSlice&#39;;<br><br>const ItemList = () =&gt; {<br>  const dispatch = useDispatch();<br>  const items = useSelector((state) =&gt; state.items.items);<br>  const [newItem, setNewItem] = useState(&#39;&#39;);<br><br>  useEffect(() =&gt; {<br>    dispatch(fetchItems());<br>  }, [dispatch]);<br><br>  const handleAddItem = () =&gt; {<br>    dispatch(addItem({ name: newItem }));<br>    setNewItem(&#39;&#39;);<br>  };<br><br>  return (<br>    &lt;div&gt;<br>      &lt;h1&gt;Item List&lt;/h1&gt;<br>      &lt;input<br>        type=&quot;text&quot;<br>        value={newItem}<br>        onChange={(e) =&gt; setNewItem(e.target.value)}<br>      /&gt;<br>      &lt;button onClick={handleAddItem}&gt;Add Item&lt;/button&gt;<br>      &lt;ul&gt;<br>        {items.map((item) =&gt; (<br>          &lt;li key={item.id}&gt;{item.name}&lt;/li&gt;<br>        ))}<br>      &lt;/ul&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default ItemList;</pre><p><strong>5. Handling Offline Actions</strong></p><p>redux-offline handles the offline scenario by queuing actions and replaying them when the network is restored. You can customize the behavior by configuring redux-offline’s settings. For example, you might want to customize the effect and discard functions to handle retries and failures differently.</p><pre>// src/app/store/index.js (updated)<br><br>const store = configureStore({<br>  reducer: {<br>    items: itemsSlice.reducer,<br>  },<br>  enhancers: [offline({<br>    ...offlineConfig,<br>    effect: (effect, _action) =&gt; axios(effect),<br>    discard: (error, _action, _retries) =&gt; {<br>      const { request, response } = error;<br>      if (!request) throw error; // There was an error creating the request<br>      if (!response) return false; // There was no response<br>      return response.status &gt;= 400 &amp;&amp; response.status &lt; 500;<br>    },<br>  })],<br>});<br><br>export default store;</pre><p>This setup ensures that your app can handle offline scenarios gracefully, providing a seamless experience for users even during network interruptions. You can further enhance this app by adding more features and customizing the offline behavior as needed.</p><p>Feel free to explore the documentation for Redux Toolkit and <a href="https://github.com/redux-offline/redux-offline">redux-offline</a> for more advanced configurations and optimizations. Happy coding!</p><h3>2. Enabling Offline Capabilities Using Service Worker</h3><p>To make our app work offline, we’ll use Workbox to cache our assets and handle network requests. Create a service worker file and configure it.</p><h4>What is a Service Worker?</h4><p>A service worker is a script that your browser runs in the background, separate from a web page, enabling features that don’t need a web page or user interaction. One of its primary uses is to intercept network requests, cache or retrieve resources from the cache, and provide an offline experience. It acts as a proxy between the web application, the browser, and the network.</p><h4>Why Use Service Workers?</h4><p>Service workers are powerful tools that:</p><ul><li>Enable offline functionality by caching assets and API responses.</li><li>Improve performance by serving cached resources, reducing the need to fetch from the network.</li><li>Allow background synchronization of data.</li><li>Handle push notifications.</li></ul><p>Start by installing the necessary libraries:</p><pre>npm install idb workbox-core workbox-precaching workbox-routing workbox-strategies</pre><h4>Configuring Redux Toolkit</h4><p>Let’s set up the Redux Toolkit for state management. Create a slice for managing our CRUD operations.</p><pre>// src/app/store/features/todos/todosSlice.js<br><br>import { createSlice, createAsyncThunk } from &#39;@reduxjs/toolkit&#39;;<br>import { openDB } from &#39;idb&#39;;<br><br>// Open IndexedDB<br>const dbPromise = openDB(&#39;todos-db&#39;, 1, {<br>  upgrade(db) {<br>    db.createObjectStore(&#39;todos&#39;, { keyPath: &#39;id&#39;, autoIncrement: true });<br>  },<br>});<br><br>// Thunks for async operations<br>export const fetchTodos = createAsyncThunk(&#39;todos/fetchTodos&#39;, async () =&gt; {<br>  const db = await dbPromise;<br>  return await db.getAll(&#39;todos&#39;);<br>});<br><br>export const addTodo = createAsyncThunk(&#39;todos/addTodo&#39;, async (todo) =&gt; {<br>  const db = await dbPromise;<br>  await db.add(&#39;todos&#39;, todo);<br>  return todo;<br>});<br><br>export const updateTodo = createAsyncThunk(&#39;todos/updateTodo&#39;, async (todo) =&gt; {<br>  const db = await dbPromise;<br>  await db.put(&#39;todos&#39;, todo);<br>  return todo;<br>});<br><br>export const deleteTodo = createAsyncThunk(&#39;todos/deleteTodo&#39;, async (id) =&gt; {<br>  const db = await dbPromise;<br>  await db.delete(&#39;todos&#39;, id);<br>  return id;<br>});<br><br>const todosSlice = createSlice({<br>  name: &#39;todos&#39;,<br>  initialState: {<br>    items: [],<br>    status: &#39;idle&#39;,<br>    error: null,<br>  },<br>  reducers: {},<br>  extraReducers: (builder) =&gt; {<br>    builder<br>      .addCase(fetchTodos.fulfilled, (state, action) =&gt; {<br>        state.items = action.payload;<br>      })<br>      .addCase(addTodo.fulfilled, (state, action) =&gt; {<br>        state.items.push(action.payload);<br>      })<br>      .addCase(updateTodo.fulfilled, (state, action) =&gt; {<br>        const index = state.items.findIndex((item) =&gt; item.id === action.payload.id);<br>        state.items[index] = action.payload;<br>      })<br>      .addCase(deleteTodo.fulfilled, (state, action) =&gt; {<br>        state.items = state.items.filter((item) =&gt; item.id !== action.payload);<br>      });<br>  },<br>});<br><br>export default todosSlice.reducer;</pre><h4>Setting Up the Store</h4><p>Configure the Redux store with our todos slice.</p><pre>// src/app/store/index.js<br><br>import { configureStore } from &#39;@reduxjs/toolkit&#39;;<br>import todosReducer from &#39;../features/todos/todosSlice&#39;;<br>const store = configureStore({<br>  reducer: {<br>    todos: todosReducer,<br>  },<br>});<br>export default store;</pre><h4>Creating Components</h4><p>Create components for displaying, adding, updating, and deleting todos.</p><pre>// src/components/TodoList.js<br><br>import React, { useEffect } from &#39;react&#39;;<br>import { useDispatch, useSelector } from &#39;react-redux&#39;;<br>import { fetchTodos, deleteTodo } from &#39;../features/todos/todosSlice&#39;;<br>const TodoList = () =&gt; {<br>  const dispatch = useDispatch();<br>  const todos = useSelector((state) =&gt; state.todos.items);<br>  useEffect(() =&gt; {<br>    dispatch(fetchTodos());<br>  }, [dispatch]);<br>  const handleDelete = (id) =&gt; {<br>    dispatch(deleteTodo(id));<br>  };<br>  return (<br>    &lt;div&gt;<br>      &lt;h2&gt;Todo List&lt;/h2&gt;<br>      &lt;ul&gt;<br>        {todos.map((todo) =&gt; (<br>          &lt;li key={todo.id}&gt;<br>            {todo.text}<br>            &lt;button onClick={() =&gt; handleDelete(todo.id)}&gt;Delete&lt;/button&gt;<br>          &lt;/li&gt;<br>        ))}<br>      &lt;/ul&gt;<br>    &lt;/div&gt;<br>  );<br>};<br>export default TodoList;</pre><pre>// src/components/AddTodo.js<br><br>import React, { useState } from &#39;react&#39;;<br>import { useDispatch } from &#39;react-redux&#39;;<br>import { addTodo } from &#39;../features/todos/todosSlice&#39;;<br><br>const AddTodo = () =&gt; {<br>  const [text, setText] = useState(&#39;&#39;);<br>  const dispatch = useDispatch();<br><br>  const handleAdd = () =&gt; {<br>    dispatch(addTodo({ text }));<br>    setText(&#39;&#39;);<br>  };<br><br>  return (<br>    &lt;div&gt;<br>      &lt;h2&gt;Add Todo&lt;/h2&gt;<br>      &lt;input<br>        type=&quot;text&quot;<br>        value={text}<br>        onChange={(e) =&gt; setText(e.target.value)}<br>      /&gt;<br>      &lt;button onClick={handleAdd}&gt;Add&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default AddTodo;</pre><h4>How to Implement a Service Worker</h4><ol><li><strong>Registering the Service Worker</strong>: This tells the browser where the service worker file is located and triggers the installation process.</li><li><strong>Installing the Service Worker</strong>: During the installation phase, you can cache the necessary assets and resources.</li><li><strong>Activating the Service Worker</strong>: After installation, the service worker activates and can start controlling the web pages and handling fetch events.</li><li><strong>Handling Fetch Events</strong>: The service worker intercepts network requests, serving cached responses when available, and fetching from the network when not.</li></ol><h4>1. Registering the Service Worker</h4><p>First, we need to register the service worker in our main JavaScript file (usually index.js or main.js).</p><pre>// src/index.js<br><br>import React from &#39;react&#39;;<br>import ReactDOM from &#39;react-dom&#39;;<br>import { Provider } from &#39;react-redux&#39;;<br>import store from &#39;./app/store&#39;;<br>import App from &#39;./App&#39;;<br>import * as serviceWorkerRegistration from &#39;./serviceWorkerRegistration&#39;;<br><br>ReactDOM.render(<br>  &lt;Provider store={store}&gt;<br>    &lt;App /&gt;<br>  &lt;/Provider&gt;,<br>  document.getElementById(&#39;root&#39;)<br>);<br><br>// Register the service worker<br>serviceWorkerRegistration.register();</pre><p>The serviceWorkerRegistration.register(); function registers the service worker script.</p><h4>2. Creating the Service Worker Script</h4><p>Create a service worker script in the public directory, usually named service-worker.js.</p><pre>// src/service-worker.js<br><br>import { precacheAndRoute } from &#39;workbox-precaching&#39;;<br>import { registerRoute } from &#39;workbox-routing&#39;;<br>import { NetworkFirst } from &#39;workbox-strategies&#39;;<br><br>// Precache all the assets generated by the build process<br>precacheAndRoute(self.__WB_MANIFEST);<br><br>// Cache HTML documents with a Network First strategy<br>registerRoute(<br>  ({ request }) =&gt; request.destination === &#39;document&#39;,<br>  new NetworkFirst()<br>);</pre><h4>3. Using Workbox</h4><p>Workbox is a set of libraries that makes it easy to handle caching and offline capabilities. In the above script, we use:</p><ul><li>precacheAndRoute: This method precaches all the assets specified in the self.__WB_MANIFEST array. The manifest is generated by the build process and includes all the files to be cached.</li><li>registerRoute: This method allows us to define caching strategies for specific types of requests. In this case, we use the NetworkFirst strategy for HTML documents. This means the service worker will try to fetch the document from the network first, falling back to the cache if the network is unavailable.</li></ul><h4>4. Creating serviceWorkerRegistration.js</h4><p>To manage the registration and lifecycle of the service worker, create a serviceWorkerRegistration.js file.</p><pre>// src/serviceWorkerRegistration.js<br><br>// This optional code is used to register a service worker.<br>// register() is not called by default.<br><br>const isLocalhost = Boolean(<br>  window.location.hostname === &#39;localhost&#39; ||<br>    // [::1] is the IPv6 localhost address.<br>    window.location.hostname === &#39;[::1]&#39; ||<br>    // 127.0.0.0/8 are considered localhost for IPv4.<br>    window.location.hostname.match(<br>      /^127(?:\.(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$/<br>    )<br>);<br><br>export function register(config) {<br>  if (process.env.NODE_ENV === &#39;production&#39; &amp;&amp; &#39;serviceWorker&#39; in navigator) {<br>    const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);<br>    if (publicUrl.origin !== window.location.origin) {<br>      // Our service worker won&#39;t work if PUBLIC_URL is on a different origin<br>      return;<br>    }<br><br>    window.addEventListener(&#39;load&#39;, () =&gt; {<br>      const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;<br><br>      if (isLocalhost) {<br>        // This is running on localhost. Let&#39;s check if a service worker still exists or not.<br>        checkValidServiceWorker(swUrl, config);<br><br>        // Add some additional logging to localhost, pointing developers to the<br>        // service worker/PWA documentation.<br>        navigator.serviceWorker.ready.then(() =&gt; {<br>          console.log(<br>            &#39;This web app is being served cache-first by a service &#39; +<br>              &#39;worker. To learn more, visit https://cra.link/PWA&#39;<br>          );<br>        });<br>      } else {<br>        // Is not localhost. Just register service worker<br>        registerValidSW(swUrl, config);<br>      }<br>    });<br>  }<br>}<br><br>function registerValidSW(swUrl, config) {<br>  navigator.serviceWorker<br>    .register(swUrl)<br>    .then((registration) =&gt; {<br>      registration.onupdatefound = () =&gt; {<br>        const installingWorker = registration.installing;<br>        if (installingWorker == null) {<br>          return;<br>        }<br>        installingWorker.onstatechange = () =&gt; {<br>          if (installingWorker.state === &#39;installed&#39;) {<br>            if (navigator.serviceWorker.controller) {<br>              // At this point, the updated precached content has been fetched,<br>              // but the previous service worker will still serve the older<br>              // content until all client tabs are closed.<br>              console.log(<br>                &#39;New content is available and will be used when all &#39; +<br>                  &#39;tabs for this page are closed. See https://cra.link/PWA.&#39;<br>              );<br><br>              // Execute callback<br>              if (config &amp;&amp; config.onUpdate) {<br>                config.onUpdate(registration);<br>              }<br>            } else {<br>              // At this point, everything has been precached.<br>              // It&#39;s the perfect time to display a<br>              // &quot;Content is cached for offline use.&quot; message.<br>              console.log(&#39;Content is cached for offline use.&#39;);<br><br>              // Execute callback<br>              if (config &amp;&amp; config.onSuccess) {<br>                config.onSuccess(registration);<br>              }<br>            }<br>          }<br>        };<br>      };<br>    })<br>    .catch((error) =&gt; {<br>      console.error(&#39;Error during service worker registration:&#39;, error);<br>    });<br>}<br><br>function checkValidServiceWorker(swUrl, config) {<br>  // Check if the service worker can be found. If it can&#39;t reload the page.<br>  fetch(swUrl, {<br>    headers: { &#39;Service-Worker&#39;: &#39;script&#39; },<br>  })<br>    .then((response) =&gt; {<br>      // Ensure service worker exists, and that we really are getting a JS file.<br>      const contentType = response.headers.get(&#39;content-type&#39;);<br>      if (<br>        response.status === 404 ||<br>        (contentType != null &amp;&amp; contentType.indexOf(&#39;javascript&#39;) === -1)<br>      ) {<br>        // No service worker found. Probably a different app. Reload the page.<br>        navigator.serviceWorker.ready.then((registration) =&gt; {<br>          registration.unregister().then(() =&gt; {<br>            window.location.reload();<br>          });<br>        });<br>      } else {<br>        // Service worker found. Proceed as normal.<br>        registerValidSW(swUrl, config);<br>      }<br>    })<br>    .catch(() =&gt; {<br>      console.log(<br>        &#39;No internet connection found. App is running in offline mode.&#39;<br>      );<br>    });<br>}<br><br>export function unregister() {<br>  if (&#39;serviceWorker&#39; in navigator) {<br>    navigator.serviceWorker.ready<br>      .then((registration) =&gt; {<br>        registration.unregister();<br>      })<br>      .catch((error) =&gt; {<br>        console.error(error.message);<br>      });<br>  }<br>}</pre><h3>Conclusion</h3><p>With these steps, you have successfully added offline capabilities to your React app using a service worker and Workbox. This ensures that your app remains functional even without an internet connection, providing a better user experience. The service worker caches assets and handles fetch requests, serving cached content when the network is unavailable.</p><p>This setup can be further customized to cache additional resources or implement different caching strategies as per your application’s requirements.</p><p>Feel free to explore and expand upon this example to suit your specific use case. Happy coding!</p><p><em>Thanks for reading! If you enjoyed reading this article, please </em><strong><em>click the 👏 button and share to help others find it!</em></strong><em> Feel free to leave a comment 💬 below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e14d5f053d92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Efficient Pagination in a High-Frequency, Distributed Environment]]></title>
            <link>https://medium.com/scalereal/efficient-pagination-in-a-high-frequency-distributed-environment-2f82cee53e9e?source=rss-70cbb7a28ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f82cee53e9e</guid>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <dc:creator><![CDATA[Pradip Bhusnar]]></dc:creator>
            <pubDate>Mon, 03 Jun 2024 10:11:46 GMT</pubDate>
            <atom:updated>2024-06-03T10:11:46.282Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Efficient Pagination in a High-Frequency, Distributed Environment" src="https://cdn-images-1.medium.com/max/1011/1*EpQ7XwQegmF_2OlKRUS5rw.png" /></figure><p>In today’s data-driven world, real-time applications like financial trading platforms, social media feeds, and live sports updates handle massive amounts of data. Ensuring a smooth user experience in such high-frequency environments is crucial. One of the core challenges here is implementing efficient pagination. Let’s delve into the most efficient techniques for handling real-time pagination in a distributed environment.</p><h4>What is Pagination?</h4><p>Pagination is a method used to divide large datasets into smaller, manageable chunks, often referred to as pages. This is essential for improving performance and user experience, as it allows users to navigate through data without loading everything at once.</p><p>In a previous blog post, <a href="https://medium.com/@PradipBhusnar">I</a> have discussed SQL Pagination Techniques: Enhancing Query Performance and Managing Memory Efficiently.<br>here is the ref link 👇</p><p><a href="https://medium.com/@PradipBhusnar/sql-for-pagination-queries-memory-and-performance-567af4913b50">SQL for pagination queries — memory and performance</a></p><h3>Why Real-Time Pagination is Challenging</h3><p>Real-time data is constantly being generated, making traditional pagination methods, which rely on pre-defined datasets, inadequate. Imagine a social media feed — new posts are constantly added, and users expect to see the latest updates without needing to refresh the entire page. In a distributed environment, where data is stored across multiple servers, efficiently fetching paginated results requires careful consideration.</p><p>Here’s what makes this challenging:</p><ol><li><strong>Data Volume</strong>: The sheer volume of data generated per second can be overwhelming.</li><li><strong>Consistency</strong>: As new data arrives, users should see a consistent view of the paginated results, even as older data is potentially removed. Ensuring that the data presented to users is consistent, especially in a distributed system.</li><li><strong>Latency</strong>: Users expect near-instantaneous updates when paginating through the data stream. Minimizing the time taken to fetch and display the data.</li><li><strong>Scalability</strong>: The system needs to handle increasing data volume without performance degradation. The system must scale efficiently to handle increased loads.</li></ol><h3>Key Concepts</h3><ol><li><strong>High-Frequency Data</strong>: Refers to data that is generated and updated rapidly, such as stock market ticks, real-time logs, or sensor data.</li><li><strong>Real-Time Pagination</strong>: The ability to fetch and display data in chunks or pages in real-time, ensuring low-latency access and smooth user experience.</li><li><strong>Distributed Environment</strong>: A system where data is distributed across multiple nodes or servers to ensure scalability, reliability, and fault tolerance.</li></ol><h3>Architectural Choices</h3><ol><li><strong>Distributed Databases</strong>: Use a distributed database like Apache Cassandra, Apache HBase, or Amazon DynamoDB to handle large volumes of data and distribute it across multiple nodes.</li><li><strong>Data Sharding</strong>: Partition data into smaller chunks (shards) to distribute the load evenly across servers.</li><li><strong>Event-Driven Architecture</strong>: Employ technologies like Apache Kafka for real-time data ingestion and processing.</li><li><strong>Indexing</strong>: Implement efficient indexing strategies to quickly locate and retrieve the required pages of data.</li><li><strong>Cache Layer</strong>: Use an in-memory cache (e.g., Redis or Memcached) to store frequently accessed data and reduce the load on the database.</li></ol><h3>Viable Solutions and Detailed Implementations</h3><h4>1. Data Ingestion</h4><ul><li><strong>Apache Kafka</strong>: Set up Kafka as the message broker to handle incoming high-frequency data. Kafka’s distributed nature and partitioning capabilities ensure that it can handle large data volumes efficiently.</li></ul><pre>kafka:<br>  brokers: [&quot;broker1:9092&quot;, &quot;broker2:9092&quot;]<br>  topic: &quot;real-time-data&quot;<br>  partitions: 10y</pre><ul><li><strong>Producers</strong>: Configure producers to send data to Kafka. Each piece of data should include a timestamp or a unique identifier to help with ordering.</li></ul><pre>from kafka import KafkaProducer<br>import json<br><br>producer = KafkaProducer(bootstrap_servers=&#39;broker1:9092&#39;)<br><br>def send_data(data):<br>    producer.send(&#39;real-time-data&#39;, json.dumps(data).encode(&#39;utf-8&#39;))</pre><h4>2. Data Storage</h4><ul><li><strong>Apache Cassandra</strong>: Use Cassandra for its high write throughput and ability to scale horizontally. Design the schema to support efficient pagination.</li></ul><pre>CREATE TABLE real_time_data (<br>    id UUID PRIMARY KEY,<br>    timestamp TIMESTAMP,<br>    value TEXT<br>) WITH CLUSTERING ORDER BY (timestamp DESC);</pre><ul><li><strong>Data Partitioning</strong>: Ensure data is partitioned based on a key, such as a timestamp, to evenly distribute it across nodes.</li></ul><pre>CREATE TABLE real_time_data_by_partition (<br>    partition_key TEXT,<br>    id UUID,<br>    timestamp TIMESTAMP,<br>    value TEXT,<br>    PRIMARY KEY (partition_key, timestamp, id)<br>);</pre><h4>3. Indexing</h4><ul><li><strong>Secondary Indexes</strong>: Use secondary indexes or materialized views to support efficient querying for pagination.</li></ul><pre>CREATE INDEX ON real_time_data (timestamp);</pre><ul><li><strong>ElasticSearch</strong>: Optionally, integrate ElasticSearch for full-text search and more complex querying capabilities.</li></ul><h4>4. Cache Layer</h4><ul><li><strong>Redis</strong>: Implement a caching layer using Redis to store the most recent pages of data. This reduces the load on Cassandra and speeds up data retrieval.</li></ul><pre>import redis<br><br>cache = redis.Redis(host=&#39;localhost&#39;, port=6379)<br><br>def cache_data(key, data):<br>    cache.set(key, json.dumps(data), ex=60)  # Cache for 60 seconds<br><br>def get_cached_data(key):<br>    cached = cache.get(key)<br>    return json.loads(cached) if cached else None</pre><h4>5. Real-Time Pagination</h4><ul><li><strong>Pagination Logic</strong>: Implement the logic to fetch data from the cache or the database, ensuring that requests are handled efficiently.</li></ul><pre>def fetch_page(page_number, page_size):<br>    start = (page_number - 1) * page_size<br>    end = start + page_size - 1<br><br>    cached_data = get_cached_data(f&#39;page_{page_number}&#39;)<br>    if cached_data:<br>        return cached_data<br><br>    query = f&quot;SELECT * FROM real_time_data LIMIT {page_size} OFFSET {start}&quot;<br>    result = cassandra_session.execute(query)<br>    data = [row for row in result]<br><br>    cache_data(f&#39;page_{page_number}&#39;, data)<br>    return data</pre><ul><li><strong>Consistency</strong>: Ensure consistency by using techniques such as Quorum reads in Cassandra, which ensures that data is read from a majority of replicas.</li></ul><pre>result = cassandra_session.execute(query, consistency_level=ConsistencyLevel.QUORUM)</pre><h3>Optimization Strategies</h3><ol><li><strong>Adaptive Caching</strong>: Dynamically adjust cache duration based on the frequency of data updates.</li><li><strong>Batch Processing</strong>: Use batch processing for data ingestion to reduce the number of write operations.</li><li><strong>Load Balancing</strong>: Employ load balancers to distribute read and write requests evenly across nodes.</li><li><strong>Monitoring and Alerts</strong>: Implement monitoring (e.g., Prometheus, Grafana) to track system performance and set up alerts for potential issues.</li></ol><h3>Conclusion</h3><p>Handling real-time pagination for high-frequency data in a distributed environment requires a combination of efficient data storage, robust indexing, and intelligent caching strategies. By leveraging distributed databases, event-driven architecture, and caching layers, you can build a scalable and responsive system capable of handling high-velocity data streams with low latency.</p><p>By adopting these strategies, your web application can maintain performance and consistency, even under the pressure of rapid data changes and high user demand.</p><p><em>Thanks for reading! If you enjoyed reading this article, please </em><strong><em>click the 👏 button and share to help others find it!</em></strong><em> Feel free to leave a comment 💬 below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f82cee53e9e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scalereal/efficient-pagination-in-a-high-frequency-distributed-environment-2f82cee53e9e">Efficient Pagination in a High-Frequency, Distributed Environment</a> was originally published in <a href="https://medium.com/scalereal">ScaleReal</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL for pagination queries — memory and performance]]></title>
            <link>https://medium.com/scalereal/sql-for-pagination-queries-memory-and-performance-567af4913b50?source=rss-70cbb7a28ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/567af4913b50</guid>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[query-optimization]]></category>
            <category><![CDATA[sql-performance]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Pradip Bhusnar]]></dc:creator>
            <pubDate>Thu, 30 May 2024 14:27:46 GMT</pubDate>
            <atom:updated>2024-05-30T14:32:33.241Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL Pagination Techniques: Enhancing Query Performance and Managing Memory Efficiently</h3><figure><img alt="SQL pagination: memory &amp; performance" src="https://cdn-images-1.medium.com/max/1024/1*s5wSjmVJ9AZHoitXpBqfZQ.png" /></figure><p>Handling pagination efficiently in SQL, especially in a high-frequency data environment, requires careful consideration of both memory usage and performance. Here’s a detailed look at how to design and implement efficient pagination queries in SQL.</p><h3>Key Techniques for Efficient SQL Pagination</h3><ol><li><strong>Limit and Offset</strong>:</li></ol><ul><li>Basic pagination using LIMIT and OFFSET is simple but can become inefficient with large offsets.</li><li>For example:</li></ul><pre>SELECT * FROM data_table<br>ORDER BY timestamp DESC<br>LIMIT 10 OFFSET 1000;</pre><ul><li><strong>Performance Issue</strong>: As the offset increases, the database has to scan more rows, which can lead to significant performance degradation.</li></ul><p><strong>2. Keyset Pagination (Seek Method)</strong>:</p><ul><li>Instead of using OFFSET, use the last fetched value as a reference for the next query.</li><li>For example:</li></ul><pre>SELECT * FROM data_table<br>WHERE timestamp &lt; :last_timestamp<br>ORDER BY timestamp DESC<br>LIMIT 10;</pre><ul><li>This method is more efficient because it avoids the need to scan rows that were already fetched.</li></ul><p><strong>3. Indexed Pagination</strong>:</p><ul><li>Ensure the columns used for ordering and filtering are indexed to speed up query execution.</li><li>For example:</li></ul><pre>CREATE INDEX idx_timestamp ON data_table (timestamp DESC);</pre><h3>Implementation Details</h3><h4>1. Basic Pagination with LIMIT and OFFSET</h4><pre>SELECT * FROM data_table<br>ORDER BY timestamp DESC<br>LIMIT 10 OFFSET 20;</pre><p><strong>Memory and Performance Considerations</strong>:</p><ul><li>For small offsets, this method is usually fast and consumes minimal memory.</li><li>As the offset grows, the query can become slow and consume more memory because the database engine has to process and discard the rows up to the offset.</li></ul><h4>2. Keyset Pagination (Seek Method)</h4><pre>-- Fetch the first page<br>SELECT * FROM data_table<br>ORDER BY timestamp DESC<br>LIMIT 10;<br><br>-- Fetch the next page using the last timestamp from the previous result<br>SELECT * FROM data_table<br>WHERE timestamp &lt; :last_timestamp<br>ORDER BY timestamp DESC<br>LIMIT 10;</pre><p><strong>Memory and Performance Considerations</strong>:</p><ul><li>This method is highly efficient for large datasets because it uses the index to directly seek the rows needed for the next page.</li><li>The performance remains consistent regardless of the page number.</li></ul><h4>3. Using Indexed Columns for Pagination</h4><ul><li>Ensure that the columns used for sorting and filtering in pagination queries are indexed.</li></ul><pre>CREATE INDEX idx_timestamp ON data_table (timestamp DESC);</pre><p><strong>Memory and Performance Considerations</strong>:</p><ul><li>Indexed columns significantly speed up query performance by allowing the database engine to quickly locate the rows needed.</li><li>Proper indexing minimizes the memory footprint as the database doesn’t need to load unnecessary rows into memory.</li></ul><h3>Advanced Techniques</h3><h4>1. Window Functions</h4><ul><li>Use window functions to provide more advanced pagination capabilities, such as total row count alongside the paginated results.</li></ul><pre>SELECT * FROM (<br>  SELECT<br>    data_table.*,<br>    ROW_NUMBER() OVER (ORDER BY timestamp DESC) as row_num<br>  FROM data_table<br>) AS subquery<br>WHERE row_num BETWEEN :start AND :end;</pre><p><strong>Memory and Performance Considerations</strong>:</p><ul><li>Window functions can be resource-intensive but are useful for cases where you need additional metadata about the results.</li><li>Ensure proper indexing and consider database-specific optimizations to handle window functions efficiently.</li></ul><h4>2. Caching</h4><ul><li>Implement caching mechanisms to store frequently accessed pages, reducing the need to repeatedly query the database for the same data.</li></ul><pre>import redis<br><br>cache = redis.Redis(host=&#39;localhost&#39;, port=6379)<br><br>def fetch_page(page_number, page_size):<br>    cache_key = f&quot;page_{page_number}&quot;<br>    cached_page = cache.get(cache_key)<br>    if cached_page:<br>        return json.loads(cached_page)<br><br>    # Calculate offset for LIMIT and OFFSET pagination<br>    offset = (page_number - 1) * page_size<br>    query = f&quot;SELECT * FROM data_table ORDER BY timestamp DESC LIMIT {page_size} OFFSET {offset}&quot;<br>    result = execute_query(query)<br><br>    # Cache the result<br>    cache.set(cache_key, json.dumps(result), ex=60)  # Cache for 60 seconds<br>    return result</pre><p><strong>Memory and Performance Considerations</strong>:</p><ul><li>Caching reduces the load on the database and improves response times for frequently accessed pages.</li><li>Cache expiration policies should be designed based on the data update frequency to ensure data freshness.</li></ul><h3>Conclusion</h3><p>Efficient pagination in SQL requires balancing memory usage and performance, particularly in high-frequency data environments. While LIMIT and OFFSET provide a simple solution, keyset pagination (seek method) is generally more efficient for large datasets. Proper indexing and caching can further enhance performance and reduce memory consumption. By carefully selecting and combining these techniques, you can achieve efficient real-time pagination in a distributed environment.</p><p><em>Thanks for reading! If you enjoyed reading this article, please </em><strong><em>click the 👏 button and share to help others find it!</em></strong><em> Feel free to leave a comment 💬 below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=567af4913b50" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scalereal/sql-for-pagination-queries-memory-and-performance-567af4913b50">SQL for pagination queries — memory and performance</a> was originally published in <a href="https://medium.com/scalereal">ScaleReal</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[10 Techniques to Improve Your React Webpack PWA]]></title>
            <link>https://medium.com/scalereal/10-techniques-to-improve-your-react-webpack-pwa-922be80e66b4?source=rss-70cbb7a28ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/922be80e66b4</guid>
            <category><![CDATA[pwa]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[workbox]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Pradip Bhusnar]]></dc:creator>
            <pubDate>Sun, 26 May 2024 12:48:29 GMT</pubDate>
            <atom:updated>2024-05-26T12:48:29.819Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rZKLFeXQ6D0m2dPTL2AYrw.png" /></figure><p>Progressive Web Apps (PWAs) have revolutionized the web development landscape, offering users fast and engaging experiences that rival native applications. If you’re building a React application with Webpack as your bundler, there are several techniques you can employ to optimize and enhance your PWA’s performance. In this blog, we’ll explore ten powerful techniques to improve your React Webpack PWA, resulting in faster load times, better caching strategies, and enhanced user experiences.</p><h4>1. Code Splitting</h4><p>By leveraging React’s built-in code-splitting capabilities, you can split your application into smaller chunks. As a result, only the necessary code is loaded when a specific component or feature is required, reducing the initial load time and improving performance.</p><h4>2. Asset Compression</h4><p>Compressing your JavaScript and CSS assets with tools like UglifyJsPlugin and OptimizeCSSAssetsPlugin can significantly reduce file sizes. Smaller assets result in faster downloads, improving load times and overall performance.</p><h4>3. Caching Strategies</h4><p>Caching strategies play a crucial role in improving the performance and offline capabilities of your React Webpack Progressive Web App (PWA). Implement effective caching strategies using tools like Workbox. you can employ the following caching strategies:</p><ol><li>Precaching: Use a service worker to precache essential assets like HTML, CSS, JavaScript, and image files during the installation phase. This ensures that the PWA can be loaded quickly, even when offline.</li><li>Runtime Caching: Implement runtime caching to cache dynamic data or resources that are fetched during runtime, such as API responses or user-generated content. This enables the PWA to function offline or with limited connectivity, providing a seamless experience.</li><li>Cache-First Strategy: Configure the service worker to serve cached assets first, checking the cache for a response before making a network request. This strategy prioritizes quick retrieval of resources from the cache, reducing the dependency on network availability.</li><li>Stale-While-Revalidate: Combine caching with network requests by serving a cached response while simultaneously making a request to update the cache. This strategy ensures that users receive a response quickly from the cache while keeping the cache up to date with fresh data.</li><li>Cache-Busting: Implement cache-busting techniques, such as appending a versioning parameter or using unique filenames for assets, to ensure that updated versions of your PWA&#39;s assets are fetched by the browser instead of serving cached versions.</li><li>Dynamic Caching: Employ dynamic caching to selectively cache specific resources based on user interactions or business logic. This allows you to optimize caching for different parts of your PWA, enhancing performance for frequently accessed or critical content.</li></ol><p>Remember to balance caching with cache invalidation strategies to ensure that users receive the most up-to-date content when changes occur. By implementing these caching strategies, you can improve the performance, offline functionality, and user experience of your React Webpack PWA.</p><h4>4. Lazy Loading</h4><p>Lazy loading non-critical components, images, and resources that are not immediately visible can further enhance performance. By deferring the loading of these elements until they are needed, you reduce the initial load time and improve the perceived performance of your PWA.</p><h4>5. PWA Audits and Optimization</h4><p>Perform regular audits using tools like Lighthouse or Chrome DevTools’ PWA section to identify areas for improvement. These tools provide suggestions for optimizing performance, accessibility, and best practices specific to PWAs.</p><h4>6. Service Worker Update Strategy</h4><p>Implement a proper service worker update strategy to ensure users receive the latest version of your app when updates are available. By using cache control headers and versioning assets, you can facilitate seamless updates without relying on the default service worker behavior.</p><p>Here’s a brief overview of how to handle service worker updates:</p><ol><li>To handle service worker updates, you need to listen for the updatefound event. This event is triggered when a new service worker is detected. You can add an event listener to the serviceWorker object to detect updates:</li></ol><pre>navigator.serviceWorker.addEventListener(&#39;updatefound&#39;, () =&gt; {<br>  // New service worker found<br>});</pre><p>2. Installing the Updated Service Worker: When the updatefound event is fired, it means that a new service worker has been found. You can access the new service worker instance using the installing property. To install the updated service worker, you can add an event listener to the installing property:</p><pre>navigator.serviceWorker.addEventListener(&#39;updatefound&#39;, () =&gt; {<br>  const newServiceWorker = navigator.serviceWorker.installing;<br><br>  newServiceWorker.addEventListener(&#39;statechange&#39;, () =&gt; {<br>    if (newServiceWorker.state === &#39;installed&#39;) {<br>      // New service worker installed<br>    }<br>  });<br>});</pre><p>3. Activating the Updated Service Worker: After the new service worker is installed, it needs to be activated to take control of the PWA. Once activated, it can start serving the updated assets and logic. You can listen for the controllerchange event to detect when the new service worker takes control:</p><pre>navigator.serviceWorker.addEventListener(&#39;controllerchange&#39;, () =&gt; {<br>  // New service worker activated<br>});</pre><p>4. Triggering an Update: To prompt users to update their PWA with the latest version, you can add a UI element or implement a mechanism that allows them to manually trigger an update. When triggered, you can call the serviceWorker.update() method to check for updates and install the new service worker:</p><pre>const updateApp = async () =&gt; {<br>  if (&#39;serviceWorker&#39; in navigator) {<br>    const registration = await navigator.serviceWorker.getRegistration();<br>    if (registration?.waiting) {<br>      registration.waiting.postMessage({ type: &#39;SKIP_WAITING&#39; });<br>    }<br>  }<br>};</pre><p>By sending the SKIP_WAITING message to the waiting service worker, you can bypass the waiting phase and activate the new service worker immediately.</p><p>Implementing a proper service worker update strategy ensures that users are seamlessly transitioned to the latest version of your PWA. It allows them to benefit from bug fixes, new features, and other improvements without disruptions.</p><h4>7. Background Sync</h4><p>Utilize background sync to allow users to perform actions while offline, such as submitting forms or queuing requests. With background sync, queued actions can be automatically synchronized when the user regains internet connectivity.</p><p>To implement Background Sync in your React Webpack PWA, follow these steps:</p><ol><li>Check for browser support: Ensure that the user’s browser supports the Background Sync API. You can use feature detection methods like navigator.serviceWorker.controller &amp;&amp; &#39;SyncManager&#39; in self to determine if the API is available.</li><li>Register a sync event: In your service worker file, register a sync event listener to listen for sync events triggered by the browser when the connection is restored. For example:</li></ol><pre>self.addEventListener(&#39;sync&#39;, (event) =&gt; {<br>  if (event.tag === &#39;sync-example&#39;) {<br>    // Perform actions or sync requests here<br>  }<br>});</pre><p>3. Queue requests for sync: In your React components, whenever a critical action or request fails due to a lack of connectivity, queue the request for sync using the Background Sync API. For example:</p><pre>if (&#39;serviceWorker&#39; in navigator &amp;&amp; &#39;SyncManager&#39; in window) {<br>  navigator.serviceWorker.ready.then((registration) =&gt; {<br>    return registration.sync.register(&#39;sync-example&#39;);<br>  });<br>}</pre><p>4. Handle sync event in the service worker: When the connection is restored and a sync event occurs, the service worker will receive the event. Handle the sync event by retrieving the queued requests and synchronizing them with the server. You can use this opportunity to update your data or retry failed requests.</p><p>By implementing Background Sync in your React Webpack PWA, you can provide a more robust and reliable user experience. Users can continue to perform critical actions and interact with your app even when they are offline, with the assurance that their data will be synchronized once the connection is available again.</p><h4>8. Push Notifications</h4><p>Engage users and provide timely updates by implementing push notification functionality. Services like Firebase Cloud Messaging (FCM) or the Push API can be integrated into your PWA, enabling you to send push notifications and keep users informed.</p><p>Here’s a general guide to help you get started:</p><ol><li>Request user permission: In your React component, request permission from the user to receive push notifications using the Notification.requestPermission() method. Handle the user&#39;s response and store it in the application state.</li><li>Generate a push notification subscription: After obtaining permission, you’ll need to generate a subscription to the push notification service (e.g., Firebase Cloud Messaging or OneSignal). This typically involves calling an API provided by the service and passing the necessary information, such as the user’s device token or registration ID.</li><li>Handle push notification events: In your service worker file, add event listeners for the push event. When a push notification is received, you can customize its display or handle any necessary actions based on the payload. You can use the self.registration.showNotification() method to display the notification.</li></ol><p>Keep in mind that the specific implementation details may vary depending on the push notification service you choose and the libraries you use in your React app. Make sure to consult the documentation of the specific tools and services you’re working with for more detailed instructions.</p><p>Additionally, it’s worth noting that implementing push notifications can be a complex task, and it requires careful consideration of user experience and privacy concerns. Ensure that you handle permissions, data payloads, and notification display in a user-friendly and secure manner.</p><h4>9. Performance Monitoring</h4><p>Continuously monitor and optimize your PWA’s performance using tools like Google Analytics, New Relic, or custom performance monitoring solutions. By tracking metrics and identifying bottlenecks, you can make data-driven optimizations for an even better user experience.</p><p>Here are some steps you can follow to set up performance monitoring:</p><ol><li>Use a performance monitoring tool: There are several performance monitoring tools available that can help you track and analyze the performance of your PWA. Some popular options include Google Analytics, New Relic, Sentry, and Datadog. Choose a tool that fits your requirements and integrate it into your application.</li><li>Measure performance metrics: Use performance APIs provided by browsers, such as the Performance API and the User Timing API, to measure critical performance metrics. These metrics include page load time, resource timing, time to interactive, and more. You can use these APIs to capture performance data and send it to your monitoring tool for analysis.</li><li>Implement error tracking: In addition to performance monitoring, it’s important to track and monitor errors that occur in your application. Utilize error-tracking tools like Sentry or Bugsnag to capture and log errors and exceptions that occur in your React app. This helps you identify and address performance issues caused by errors or faulty code.</li><li>Set up performance budgets: Define performance budgets to establish thresholds for key performance metrics. These budgets act as benchmarks to ensure your app meets certain performance goals. For example, you can set a budget for the maximum allowed page load time or the maximum size of JavaScript bundles. Continuously monitor and enforce these budgets during development to catch any performance regressions early on.</li><li>Optimize your webpack configuration: Webpack plays a crucial role in optimizing the performance of your app. Configure webpack to enable features like code splitting, tree shaking, and minification to reduce the size of your JavaScript bundles. Use webpack plugins like TerserPlugin or BabelMinifyPlugin to minify and compress your code. These optimizations can significantly improve the loading and rendering speed of your app.</li><li>Analyze performance data: Regularly review the performance data collected by your monitoring tool. Look for trends, identify bottlenecks, and optimize specific areas of your app that are causing performance issues. Pay attention to slow-loading resources, excessive network requests, or JavaScript code that might be blocking the main thread.</li><li>Continuously monitor and iterate: Performance monitoring is an ongoing process. Keep monitoring the performance of your PWA as you make changes and updates. Use the insights gained from monitoring to optimize your app further and ensure it continues to meet your performance goals.</li></ol><p>Remember that monitoring performance is a continuous effort, and it’s important to regularly review and optimize your app based on the insights gained from monitoring. This iterative process helps you maintain a high-performance PWA that delivers a smooth and responsive user experience.</p><h4>10. Accessibility</h4><p>Ensure your PWA is accessible to all users by following accessibility guidelines and best practices. Use semantic HTML, provide alternative text for images, and test your app with tools like Lighthouse to improve accessibility and make your PWA inclusive. Accessibility is crucial to make your application usable for a diverse range of users.</p><p>Here are some steps you can follow to improve the accessibility of your PWA:</p><ol><li>Use semantic HTML: When developing your React components, make sure to use appropriate HTML elements that convey the correct meaning and structure of your content. Use headings (&lt;h1&gt; to &lt;h6&gt;), lists (&lt;ul&gt;, &lt;ol&gt;, &lt;li&gt;), and other semantic elements to provide a clear document structure. This helps screen readers and assistive technologies understand and navigate your app correctly.</li><li>Provide meaningful alternative text for images: Include alt attributes in your &lt;img&gt; tags to provide alternative text that describes the content of the image. The alternative text should be concise and descriptive, conveying the purpose and context of the image to users who cannot see it.</li><li>Ensure keyboard accessibility: Ensure that all interactive elements in your app, such as buttons, links, and form inputs, can be accessed and operated using the keyboard alone. Test your app’s keyboard navigation and ensure that the focus is visible and moves in a logical order. Use the tabindex attribute and appropriate keyboard event handlers (onKeyDown, onKeyUp, etc.) to handle keyboard interactions.</li><li>Provide clear and descriptive labels: Associate labels with form inputs using the &lt;label&gt; element. This helps users understand the purpose of the input and improves accessibility for users with assistive technologies. For checkboxes and radio buttons, use the for attribute on the &lt;label&gt; element to associate it with the corresponding input element.</li><li>Ensure color contrast: Make sure that the text and interactive elements in your app have sufficient color contrast against their background to ensure readability for users with visual impairments. Use tools like WebAIM’s Color Contrast Checker or browser extensions to evaluate and adjust the color contrast of your app.</li><li>Test with screen readers and assistive technologies: Use screen readers like VoiceOver (for macOS/iOS), NVDA (for Windows), or TalkBack (for Android) to test your app’s accessibility. Navigate through your app using only the keyboard and listen to how screen readers interpret and convey the content. Identify any issues or inconsistencies and make necessary improvements.</li><li>Include ARIA attributes when needed: Accessible Rich Internet Applications (ARIA) attributes can be used to enhance the accessibility of interactive elements and custom components. Use ARIA attributes like aria-label, aria-describedby, and aria-labelledby to provide additional context and instructions for assistive technologies.</li><li>Provide error handling and feedback: Ensure that error messages and validation feedback are accessible and clearly communicated to all users. Use appropriate ARIA roles and live regions to alert users to changes in content dynamically.</li><li>Test and validate your app: Use accessibility testing tools like aXe, Wave, or Lighthouse to automatically scan your app for common accessibility issues. These tools can provide insights and suggestions for improving accessibility in your PWA.</li><li>Educate your team: Promote accessibility awareness among your development team. Provide training and resources on best practices, accessibility guidelines, and the importance of inclusive design. Encourage ongoing accessibility reviews and audits as part of your development process.</li></ol><p>By following these steps and incorporating accessibility considerations into your development workflow, you can make your React app bundled with webpack and transformed into a PWA more accessible to a wider audience, creating a better user experience for all users.</p><h4>Conclusion</h4><p>Improving your React Webpack PWA involves a combination of techniques, ranging from optimizing asset loading and caching to enhancing user experiences through features like push notifications and background sync. By implementing these ten techniques, you can create a high-performing PWA that delivers an outstanding user experience and keeps your users engaged. Embrace the power of PWAs and leverage the capabilities of React and Webpack to take your application to the next level.</p><p><em>Thanks for reading! If you enjoyed reading this article, please </em><strong><em>click the 👏 button and share to help others find it!</em></strong><em> Feel free to leave a comment 💬 below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=922be80e66b4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scalereal/10-techniques-to-improve-your-react-webpack-pwa-922be80e66b4">10 Techniques to Improve Your React Webpack PWA</a> was originally published in <a href="https://medium.com/scalereal">ScaleReal</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Progressive Web App]]></title>
            <link>https://medium.com/scalereal/react-progressive-web-app-c55ed05636da?source=rss-70cbb7a28ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c55ed05636da</guid>
            <category><![CDATA[workbox]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[pwa]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[service-worker]]></category>
            <dc:creator><![CDATA[Pradip Bhusnar]]></dc:creator>
            <pubDate>Thu, 23 Mar 2023 12:17:20 GMT</pubDate>
            <atom:updated>2023-03-23T12:25:03.840Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ti7qjwWnjxh_SWKB5w5neQ.png" /></figure><p><em>How to create PWA apps with React for specific screens or routes or dynamic routes.</em></p><p>In this article, we are going to learn what is PWA, how to create a PWA app with react using CRA, how we can transform react app built on <strong>webpack/vite</strong> to PWA, then we’ll look into how we can create PWA for specific screens, routes, or dynamic routes.</p><h4><strong>What is Progressive Web App (PWA)?</strong></h4><p>A Progressive Web App(PWA) is a more dynamic web application that can run standalone. These apps are developed using specific technologies and standard patterns to allow them to take advantage of both web and native app features. For example, web apps are more discoverable than native apps, it’s a lot easier and faster to visit a website than to install an application, and you can also share web apps by sending a link. It can be accessed over the web but also surface on the home screen of your Android or iOS device.</p><p>Progressive Web Apps (PWA) are built and enhanced with modern APIs to deliver enhanced capabilities, reliability, and installability.</p><p>PWA offers benefits such as:</p><ul><li>performance</li><li>the ability to work with or without an internet connection;</li><li>platform-specific; and,</li><li>installable.</li></ul><h3>Create a React App with the Progressive Web App Template</h3><p>To create our PWA, we’re going to use <a href="https://create-react-app.dev/">Create React App</a>. This excellent project has long had inbuilt support for making PWAs.</p><p>From Create upwards, it is possible to create a /src/serviceWorker.js to benefit from the inbuilt support for Workbox’s InjectManifest plugins, which will compile our service workers and inject the URLs for pre-caching. The easiest way to get started creating a new PWA is to use one of the <a href="https://create-react-app.dev/docs/custom-templates/">custom templates</a> offered by Facebook using Create React App command.</p><p>Let’s build the PWA starter app by running the following command:</p><pre>npx create-react-app pwa-react-typescript --template cra-template-pwa-typescript</pre><p>The above command can be broken down as follows:</p><ul><li><strong>create-react-app</strong>: This command initiates the popular Create React App utility that helps to build the starter app.</li><li><strong>name-of-our-PWA-app</strong>: This is the name of our application. You can name the app whatever you would like to.</li><li><strong>— template</strong>: This is an argument, using this you can specify a specific template for our starter react application.</li><li><strong>cra-template-pwa</strong>: The name of the PWA template for your PWA react application.</li></ul><p>This command will build your starter PWA React application. After the successful creation of the starter app, there are a few files that we shall pay attention to when it comes to PWA:</p><ul><li><strong>service-worker.js</strong>: This is a script that runs in the background once your application starts running. The service worker makes sure that you can use your React application offline and handle multiple requests for the UI. (<a href="https://developers.google.com/web/fundamentals/primers/service-workers?hl=en">learn more here</a>)</li><li><strong>manifest.json</strong>: This is basically a configuration file that tells the browser about your web application. this is essentially the metadata about the app. It can determine things such as icons, names, and colors to use when the application is displayed.</li><li><strong>serviceWorkerRegistration.js</strong>: This file tells whether or not your service worker was successfully registered.</li></ul><p>Our starter app setup is done. We can test it using the following command:</p><pre>yarn start<br><br>or<br><br>npm start</pre><p>You can add some functionality to the app ex. Counter component.</p><p>In order to opt-in to the offline-first behavior, we should look for the following in their <a href="https://github.com/cra-template/pwa/blob/master/packages/cra-template-pwa/template/src/index.js">src/index.js</a> file:</p><pre>// If you want your app to work offline and load faster, you can change<br>// unregister() to register() below. Note this comes with some pitfalls.<br>// Learn more about service workers: https://cra.link/PWA<br>serviceWorkerRegistration.unregister();</pre><p>As the comment states, switching serviceWorker.unregister() to serviceWorker.register() will opt you into using the service worker, a key benefit when it comes to PWAs. Check Google’s explanation on this for more information: <a href="https://developers.google.com/web/fundamentals/primers/service-workers/registration">https://developers.google.com/web/fundamentals/primers/service-workers/registration</a></p><p>With the PWA files setup, in order to view the difference, we can deploy the application to a static server using the serve package. You can now build the application, and can use the following command to install, build and deploy the application:</p><pre>npm install serve<br>npm run build<br>serve -s build //if you encounter an error with this command, try &#39;npx serve -s build&#39;</pre><p>You should see the message in your terminal, having your application deployed to <a href="http://localhost:5000/">http://localhost:5000</a>. If you are using VS Code Ctrl+click will open the application in a browser.</p><p>To check more details about the PWA, please open the dev tools (for Google Chrome, entering F12 opens the Dev Tools). Under the Application tab, on the left side, you should see a Service Workers section. Here we can check service-worker.js the file and is activated/running. In the Manifest section, we can check the PWA details that got added by the CRA template setup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a2V0-7GptgMPMyr_9-lBMw.png" /></figure><p>With your PWA app running using React, we’ve successfully built our first PWA React application! From here, you can test many PWA features, such as installation, offline viewing, and Lighthouse audit testing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uzm3SCUVzeyIYOpKIe41Zg.png" /></figure><h4>Customizing the PWA with React</h4><p>Open the index.html replace the title &lt;title&gt;React App&lt;/title&gt; with &lt;title&gt;React PWA&lt;/title&gt;. You can give any suitable name to your app.</p><p>Next, open up manifest.json and replace the following:</p><pre>&quot;short_name&quot;: &quot;React App&quot;,<br>&quot;name&quot;: &quot;Create React App Sample&quot;,</pre><p>With this:</p><pre>&quot;short_name&quot;: &quot;React PWA&quot;,<br>&quot;name&quot;: &quot;React PWA&quot;,</pre><p>You can modify other metadata or can use the below properties (and others not yet configured) to control how your app behaves. Here’s what it should look like right now:</p><pre>{<br>  &quot;short_name&quot;: &quot;My PWA&quot;,<br>  &quot;name&quot;: &quot;My PWA&quot;,<br>  &quot;icons&quot;: [<br>    {<br>      &quot;src&quot;: &quot;favicon.ico&quot;,<br>      &quot;sizes&quot;: &quot;64x64 32x32 24x24 16x16&quot;,<br>      &quot;type&quot;: &quot;image/x-icon&quot;<br>    },<br>    {<br>      &quot;src&quot;: &quot;logo192.png&quot;,<br>      &quot;type&quot;: &quot;image/png&quot;,<br>      &quot;sizes&quot;: &quot;192x192&quot;<br>    },<br>    {<br>      &quot;src&quot;: &quot;logo512.png&quot;,<br>      &quot;type&quot;: &quot;image/png&quot;,<br>      &quot;sizes&quot;: &quot;512x512&quot;<br>    }<br>  ],<br>  &quot;start_url&quot;: &quot;.&quot;,<br>  &quot;display&quot;: &quot;standalone&quot;,<br>  &quot;theme_color&quot;: &quot;#000000&quot;,<br>  &quot;background_color&quot;: &quot;#ffffff&quot;<br>}</pre><p>When a user adds a web app to their home screen using Chrome or Firefox on Android, the metadata in manifest.json determines what icons, names, and branding colors to use when the web app is displayed.</p><p><a href="https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/">The Web App Manifest guide</a> provides more context about what each field means, and how your customizations will affect your users’ experience.</p><h3>Transform React App Build On Webpack to PWA</h3><p>Create the manifest.json file in the public folder, and add all the required properties. Then embed the manifest file index.html using the link, add the below line of code in the &lt;head&gt; tag:</p><pre>&lt;link rel=&quot;manifest&quot; href=&quot;/manifest.json&quot; /&gt;</pre><p>To generate the Service Worker, we will have to add webpack’s Workbox plugin in our webpack.config.js file.</p><pre>npm install workbox-webpack-plugin --save-dev</pre><p>Then, in your webpack.config.js, add the following lines of code that start with a +:</p><pre>  const path = require(&#39;path&#39;);<br>  const CopyPlugin = require(&#39;copy-webpack-plugin&#39;);<br>  const HtmlWebpackPlugin = require(&#39;html-webpack-plugin&#39;);<br>  const { CleanWebpackPlugin } = require(&#39;clean-webpack-plugin&#39;);<br>  const TsconfigPathsPlugin = require(&#39;tsconfig-paths-webpack-plugin&#39;);<br>+ const WorkboxPlugin = require(&#39;workbox-webpack-plugin&#39;);<br>  <br>  const PACKAGE_ROOT_PATH = process.cwd(),<br>  SRC = path.resolve(PACKAGE_ROOT_PATH, &#39;./src&#39;),<br>  DIST = path.resolve(PACKAGE_ROOT_PATH, &#39;./dist&#39;),<br>  PUBLIC = path.resolve(PACKAGE_ROOT_PATH, &#39;./public&#39;),<br>  INDEX_HTML = path.resolve(PACKAGE_ROOT_PATH, &#39;./public/index.html&#39;);<br><br>  module.exports = {<br>    ...<br>    plugins: [<br>      new CleanWebpackPlugin(),<br>      new HtmlWebpackPlugin({<br> -      title: &#39;Output Management&#39;,<br> +      title: &#39;Progressive Web Application&#39;,<br>        template: INDEX_HTML,<br>        minify: {<br>            collapseWhitespace: true,<br>            removeComments: true<br>        }<br>      }),<br> +    new WorkboxPlugin.GenerateSW({<br> +     // these options encourage the ServiceWorkers to get in there fast<br> +      // and not allow any straggling &quot;old&quot; SWs to hang around<br> +      clientsClaim: true,<br> +      skipWaiting: true,<br> +    }),<br>      new CopyPlugin({<br>            patterns: [<br>                {<br>                    from: PUBLIC,<br>                    to: DIST,<br>                    globOptions: {<br>                        ignore: [&#39;**/index.html&#39;]<br>                    }<br>                }<br>            ]<br>       }),<br>    ],<br>  };</pre><p>With the above addition, it will generate the Service Worker, and also expose the manifest file so that it can be accessed by the web browser.</p><p>When we run: npm build or yarn build in the project terminal:</p><pre>...<br>             index.html  254 bytes          [emitted]<br>precache-manifest.b5ca1c555e832d6fbf9462efd29d27eb.js  268 bytes          [emitted]<br>      service-worker.js       1 kB          [emitted]<br>...</pre><p>We can see, we now have 2 extra files being generated; service-worker.js and the more verbose precache-manifest.b5ca1c555e832d6fbf9462efd29d27eb.js. service-worker.js is the Service Worker file and precache-manifest.b5ca1c555e832d6fbf9462efd29d27eb.js is a file that service-worker.js requires so it can run.</p><h4>Register Your Service Worker</h4><p>Open the index.js file which is the entry point of our app, add the following codes to register the Service Worker.</p><pre>  import React from &#39;react&#39;;<br>  import ReactDom from &#39;react-dom&#39;;<br>  import App from &#39;./App&#39;;<br><br>  ReactDom.render(&lt;App /&gt;, document.getElementById(&#39;root&#39;));<br><br>+ if (&#39;serviceWorker&#39; in navigator) {<br>+   window.addEventListener(&#39;load&#39;, () =&gt; {<br>+     navigator.serviceWorker.register(&#39;/service-worker.js&#39;)<br>+    .then(registration =&gt; {<br>+       console.log(&#39;SW registered: &#39;, registration);<br>+     }).catch(registrationError =&gt; {<br>+       console.log(&#39;SW registration failed: &#39;, registrationError);<br>+     });<br>+   });<br>+ }</pre><p>At this point, we have successfully generated and registered a Service Worker for the background activities on the browser. Once more run thenpm run build to build a version of the app including the registration code. Now we can run our app to see the beauty of a PWA. You can serve the build using the following command:</p><pre>npx serve -s build<br>or<br>npx serve -s dist</pre><h3>Customize PWA to work on only specific screens or Routes</h3><p>To achieve this we will mainly interact withmanifest.json file. If you check the properties of the manifest.json , there is a property called start_url .</p><h4>start_url</h4><p>The start_url tells the browser where the application should start when it is launched, and also it prevents the app from starting on whatever page the user was on when they added the app to their home screen. It is used to direct the user straight into that location.</p><p>Suppose you have added a URL as&quot;start_url&quot;: &quot;/apps/counter-app” , also you have other routes ex. / ,/dashbord and/apps . In this case, the user will be directed to the /apps/counter-app once they open the app. Think about what the user will want to do once they open the app, and place them there.</p><p>Go to the manifest.json which is present in the public folder, edit the line&quot;start_url&quot;: &quot;.” and replace &quot;.” it with the URL that you want.</p><pre>{<br>  ...<br>  &quot;start_url&quot;: &quot;/apps/counter-app&quot;,<br>  ...<br>}</pre><p>This will show the PWA download icon on specific screens or routes only. In this case, the PWA download icon will be shown when the user goes to the /apps/counter-app location.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*vbn6p7ntV1VuBkQre94nVQ.png" /></figure><h4>Show the PWA download icon on the dynamic route</h4><p>Whenever the browser reads the manifest.json file and If it reads the start_url , it shows the download icon on the right side of the browser URL on that provided location. By default, it will show it on the landing screen of the app which refers to the &quot;start_url: “.”</p><p>To change this behavior or to show this icon only on specific screens or routes, we will need to do little changes in the manifest file. Remove the &quot;start_url: “.”, line from the manifest.json file, and add the id in the link tag. Later we will use that id to modify the JSON content.</p><pre>&lt;link rel=&quot;manifest&quot; id=&quot;manifest-id&quot; href=&quot;/manifest.json&quot; /&gt;</pre><p>Once that is done, now we need to embed the manifest file dynamically when that specific screen renders with the updated properties.</p><p>Suppose you want to show the PWA feat or PWA download icon on /apps/counter-app this route, then you need to attach manifest.json file in the index on CouterAppPage component. Add the following lines in the component. That will do the work.</p><pre>export const CouterAppPage:FC = () =&gt; {<br>  ...<br>  const manifestData = {<br>      short_name: &#39;Counter App&#39;,<br>      name: `Counter PWA`,<br>      icons: [<br>          {<br>              src: `${window.location.protocol}//${window.location.host}/images/android-chrome-512x512.png`,<br>              sizes: &#39;512x512&#39;,<br>              type: &#39;image/png&#39;<br>          },<br>          {<br>              src: `${window.location.protocol}//${window.location.host}/images/android-chrome-192x192.png`,<br>              sizes: &#39;192x192&#39;,<br>              type: &#39;image/png&#39;<br>          }<br>      ],<br>      start_url: window?.location.href || &#39;&#39;,<br>      display: &#39;standalone&#39;,<br>      theme_color: &#39;#aaffaa&#39;,<br>      background_color: &#39;#ffffff&#39;<br>  },<br>  manifestContent = encodeURIComponent(JSON.stringify(manifestData)),<br>  manifestUrl = `data:application/manifest+json,${manifestContent}`;<br>  <br>  useEffect(() =&gt; {<br>        const linkTag = document.getElementById(&#39;manifest-id&#39;) as HTMLLinkElement;<br>        if (manifestUrl &amp;&amp; linkTag) linkTag.href = manifestUrl;<br>    }, [manifestUrl]);<br>  ...<br>};</pre><blockquote>Note: Don’t forget to replace the manifestData content with your app content.</blockquote><p>If your application routes are dynamic, suppose your appending dynamic id into the route URL, even in that case the above lines of code will work. Because we are adding window.location.href , this will add the current URL. Only we need to pay attention to the PWA names and icons that we need to change based on the dynamic route.</p><p>With this in place, your app is now ready to run. Once more build your application and serve by using the serve -s build command. Now you can test the application, and go to that route for which we dynamically attached the manifest, once you redirect to that router you will see the download PWA icon will appear in the URL. By clicking on it you can download the application and test it out on a desktop or ios device.</p><h3>Transform React App Build On Vite to PWA</h3><p>To transform the application to PWA we’ll require vite-plugin-pwa , this will help you add PWA with almost zero configuration. <br>The plugin will add default configurations for common use cases:</p><ul><li>generate the <a href="https://developer.mozilla.org/en-US/docs/Web/Manifest">Web App Manifest</a>: using the manifest plugin option</li><li>configure the <a href="https://developer.mozilla.org/en-US/docs/Web/Manifest">Web App Manifest</a> on your application entry point: adding a link on the head on your entry point</li><li>generate the service worker: using the strategies plugin option (for more information read <a href="https://vite-pwa-org.netlify.app/guide/service-worker-strategies-and-behaviors.html#service-worker-strategies">Service Worker Strategies</a> section)</li><li>generate the script to register the service worker in the browser: using injectRegister plugin option (for more information read <a href="https://vite-pwa-org.netlify.app/guide/register-service-worker.html">Register Service Worker</a> section)</li></ul><p>To install the vite-plugin-pwa plugin use the below command:</p><pre>yarn add vite-plugin-pwa -D</pre><p>Edit your vite.config.js / vite.config.ts file and add the vite-plugin-pwa:</p><pre>import { VitePWA } from &#39;vite-plugin-pwa&#39;<br>export default defineConfig({<br>  plugins: [<br>    VitePWA({<br>      registerType: &#39;autoUpdate&#39;,<br>      /* if you are using vite-plugin-pwa plugin version prior to 0.12.2<br>        add below lines to fix injectRegister bug.<br>      */<br>      workbox: {<br>        clientsClaim: true,<br>        skipWaiting: true<br>      },<br>      /* If you want to check it on dev, add devOptions.<br>      */<br>      devOptions: {<br>        enabled: true<br>      }<br>    })<br>  ]<br>})</pre><p>by adding above lines, your application is now able to generate the <a href="https://developer.mozilla.org/en-US/docs/Web/Manifest">Web App Manifest</a> and inject it at the entry point, generate the service worker and register it in the browser.<br>Please refer <a href="https://github.com/vite-pwa/vite-plugin-pwa/blob/main/src/types.ts">types.ts</a> file for more information on vite-plugin-pwa configurations.</p><blockquote><strong>vite-plugin-pwa</strong> plugin uses <a href="https://developer.chrome.com/docs/workbox/reference/workbox-build/">workbox-build</a> node library to build the service worker, you can find more information in the <a href="https://vite-pwa-org.netlify.app/guide/service-worker-strategies-and-behaviors.html">Service Worker Strategies And Behaviors</a> and <a href="https://vite-pwa-org.netlify.app/workbox/">Workbox</a> sections.</blockquote><h3>Conclusion</h3><p>So far we have learned about what is PWA, how we can setup PWA with react using CRA templates, customize the PWA by modifying manifest.json file, transforming react app build with <strong>webpack/vite</strong> to PWA, how to config webpack for generating service-worker, and customization of PWA to work on specific screens or routes.</p><p><em>Thanks for reading! If you enjoyed reading this article, please </em><strong><em>click the 👏 button and share to help others find it!</em></strong><em> Feel free to leave a comment 💬 below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c55ed05636da" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scalereal/react-progressive-web-app-c55ed05636da">React Progressive Web App</a> was originally published in <a href="https://medium.com/scalereal">ScaleReal</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Manage AWS S3 files in React front-end with the help of Okta Securely]]></title>
            <link>https://medium.com/scalereal/manage-aws-s3-files-in-react-front-end-with-the-help-of-okta-securely-c61f5c68f465?source=rss-70cbb7a28ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/c61f5c68f465</guid>
            <category><![CDATA[file-upload]]></category>
            <category><![CDATA[aws-s3]]></category>
            <category><![CDATA[openid-connect]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[okta]]></category>
            <dc:creator><![CDATA[Pradip Bhusnar]]></dc:creator>
            <pubDate>Thu, 20 Oct 2022 05:19:26 GMT</pubDate>
            <atom:updated>2022-10-20T11:56:25.086Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ZeX_5aGgnxyl-iH5JbR8LA.png" /></figure><p><em>How to upload or list files from the s3 bucket using okta credentials.</em></p><p><em>We’ll be integrating okta, SPA, and AWS. I have built an app that uses okta open id connect to authenticate and access s3 bucket resources.</em></p><h3>Background</h3><p>For one of our client projects at ScaleReal, We used Okta for authentication and authorization. We came across a use case where we wanted to upload files to S3 buckets securely, Allowing only authenticated users to upload the files. After doing some research we learned that there are two ways we can handle it</p><ol><li>Let the backend handle the authentication and upload by creating the REST API</li><li>Let the frontend handle the upload without involving the backend.</li></ol><p>We chose the latter one.</p><h3>Prerequisites</h3><p>To implement this we will need the following:</p><ul><li>An Open ID Connect app in okta (for now we’ll be using a developer account).</li><li>An AWS S3 bucket, If you don’t have an account you can get started with the trial account.</li></ul><h3>Creating Web App in Okta</h3><p>The first task is to create the web app in <a href="https://developer.okta.com/"><strong><em>Okta</em></strong></a> and get the <strong>Client Id</strong> (sometimes referred it as ‘audience’) and <strong>Okta org URL </strong>(Auth issuer).</p><p>Please follow the below steps to create the web app in okta:</p><ol><li><a href="https://developer.okta.com/signup/">Log in / sign up</a> to okta org.</li><li>Select the <strong>Applications &gt; Applications</strong> section on the right sidebar of the screen.</li><li>Click on Create App Integration</li></ol><figure><img alt="create app integration" src="https://cdn-images-1.medium.com/max/1024/1*iDRDpD9tWXBR03ghXjZPgA.png" /></figure><p>4. Select the Sign-In method as <strong>OIDC — OpenID</strong> Connect and the application type as <strong>Single-Page Application</strong>.</p><figure><img alt="App Integration popup" src="https://cdn-images-1.medium.com/max/942/1*cQiTWg2FW46fgYXb5xAz3Q.png" /></figure><p>5. Click on the next button it will redirect to <strong>New Single-Page App Integration</strong> screen.</p><ul><li>Add <strong>App integration name</strong></li><li>Select <strong>Grant type</strong> as Authorization Code and Refresh Token</li><li>Add S<strong>ign-in redirect URIs</strong> ( eg. <a href="http://localhost:3001/login/callback">http://localhost:</a><a href="http://localhost:8080/">8080</a><a href="http://localhost:3001/login/callback">/login/callback</a> ) and <strong>Sign-out redirect URIs</strong> ( eg. <a href="http://localhost:3001/login">http://localhost:</a><a href="http://localhost:8080/">8080</a><a href="http://localhost:3001/login">/login</a> )</li><li>Add <strong>Trusted Origins</strong> ( eg. <a href="http://localhost:8080/">http://localhost:8080</a> )</li><li>Add <strong>Controlled access</strong> as per your need.</li><li>Click on the save button.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*wfqHqMfBWgPHQggmzoO6kw.png" /></figure><p>6. Make note of your <strong>Client ID</strong> and <strong>Okta org URL (Auth issuer)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mtREqd3Nsb3hCWu0tJjmGQ.png" /></figure><h3>Integrating okta with React App</h3><p>Please follow the below steps to integrate your Single Page Application</p><p>1. Create your react app</p><p>2. Add packages</p><pre>yarn add @okta/okta-react@6.4 @okta/okta-auth-js@6.0</pre><p>3. Add login component and wrap the component <strong>withOktaAuth </strong>to get access to <strong>oktaAuth</strong> and <strong>authState</strong> props. Using these props we can sign in to the application and can check the authentication status of our app. Also, we can restrict the login route.</p><p>I have added the login component snippet below you can refer to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba61da047d27fdd9841585f39216ba64/href">https://medium.com/media/ba61da047d27fdd9841585f39216ba64/href</a></iframe><p>4. Create a landing screen for your app (eg. Dashboard).</p><p>5. Add routes setup and configure the okta</p><p>To configure the okta with the app we require information from the app integration that we created earlier to configure communication with the API: Client ID and Issuer.<br>The Okta React SDK requires an instance of an OktaAuth object with configuration properties. You need to set the clientId and issuer .</p><blockquote>Note: create .env file with <strong>APP_CLIENT_ID</strong>, and <strong>AUTH_ISSUER (</strong>should be your Okta org URL eg. ‘<a href="https://example.okta.com),/">https://example.okta.com</a>’<strong>)</strong></blockquote><p>I have added the configuration in the routes file, you can refer to below code snippet.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8998ee758a3897cb4a6937cb1e2347b9/href">https://medium.com/media/8998ee758a3897cb4a6937cb1e2347b9/href</a></iframe><p>6. Add logout and get profile info</p><p>The okta react sdk provides the <strong>useOktaAuth</strong> hook which provides <strong>oktaAuth </strong>and<strong> authState</strong> variables. These are used to sign out the user and also to get the claim details which include profile info and other details.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77b1a27a566ce471d8be1d75bca17b87/href">https://medium.com/media/77b1a27a566ce471d8be1d75bca17b87/href</a></iframe><h3>Integrating okta with AWS</h3><h4>Create an Amazon S3 bucket and configure CORS</h4><ol><li>Go to the <a href="https://console.aws.amazon.com/s3/home">Amazon S3 console</a></li><li>Select the existing bucket or create a new bucket. Note the bucket name and region for later use.</li><li>Go into the bucket and open the <strong>Permission</strong> section, and click <strong>Edit CORS Configuration<br></strong>Copy the below <strong>JSON</strong> and paste it into the edit box and <strong>save the changes<br></strong>You can expose the file metadata info that you passed with the file in the <strong>ExposeHeaders</strong> section.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc097c145370f204b83d4832c33e435a/href">https://medium.com/media/cc097c145370f204b83d4832c33e435a/href</a></iframe><h4>Create an IAM OpenID Connect Provider</h4><ol><li>Go to the <a href="https://console.aws.amazon.com/iam/home?#/providers">IAM console</a> and click the Add Provider button, select OpenID Connect, enter your Okta org URL as the Provider URL ( e.g. <a href="https://example.okta.com),/">https://example.okta.com ),</a> and click on Get thumbprint. Enter your Okta app <em>Client ID </em>as the Audience, and click Add Provider button. This will create a new identity provider and this will help to manage your user identities outside of AWS.</li></ol><h4>Create an IAM Role and Assign Users Logged in through Okta</h4><ol><li>Go to the <strong>Policies</strong> section of the <a href="https://console.aws.amazon.com/iam/home#policies">IAM console</a> and click <strong>Create Policy</strong> button to create a new policy.</li><li>Switch to the <strong>JSON</strong> tab and add the below JSON. Please make sure you replace the YOUR_BUCKET_NAME and YOUR_OIDC_PROVIDER_URL from the JSON.</li><li>Click on <strong>Next</strong> and add the name for your policy (e.g. okta-policy) and finally, click on <strong>Create policy</strong> button.</li><li>Now go to the <strong>Roles</strong> section of the <a href="https://console.aws.amazon.com/iam/home#/roles">IAM console</a> and click <strong>Create Role</strong> to create a new role.</li><li>Select the trusted entity type as <strong>Web identity,</strong> and select your okta org from the <strong>Identity provider</strong> drop-down list. Then select the <strong>audience</strong> from the drop-down. And click on the <strong>Next</strong> button.</li><li>On the <strong>Add Permission (Attach Policy)</strong> step, select the policy you just created (e.g. okta-policy), and click <strong>Next Step.</strong></li><li>Add role name, edit select trusted entities, and add the below JSON then click on <strong>Create Role</strong> button.</li><li>Use the JSON below for policy.</li></ol><blockquote>Make sure that you replace <strong>YOUR_BUCKET_NAME</strong> and <strong>YOUR_OIDC_PROVIDER_URL</strong> in the policy.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3ba3fa88392eb598c397f3134147c20/href">https://medium.com/media/a3ba3fa88392eb598c397f3134147c20/href</a></iframe><blockquote><strong>Important</strong>: make sure you not down the <strong>ARN</strong> from role summary. Then <strong>bucket region</strong>, <strong>bucket name</strong> Those will be used as <strong>AWS_ROLE_ARN</strong> , <strong>AWS_REGION</strong> and <strong>AWS_S3_BUCKET_NAME</strong> .</blockquote><h3>Let&#39;s get started with the operations.</h3><h4>Libraries Used</h4><p>@aws-sdk/client-s3</p><p>@aws-sdk/credential-providers</p><h4>Create Credential and S3 Client</h4><p>To create or assume credentials for S3Client object we use fromWebToken method from @aws-sdk/credential-providers which accepts objects containing roleArn and webIdentityToken.</p><p>Credentials and roles are assumed for users using the application so that file upload can be directly done without telling AWS user’s details</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68ad9c4e0d66dc9e884d9e1958a872e1/href">https://medium.com/media/68ad9c4e0d66dc9e884d9e1958a872e1/href</a></iframe><p>To create an S3 client object We use S3Client method from @aws-sdk/client-s3 which accepts objects containing region and credentials.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf95e66cf99dde4554a15b272dc43d59/href">https://medium.com/media/bf95e66cf99dde4554a15b272dc43d59/href</a></iframe><p>S3Client object can be used for uploading, deleting and listing, and many other operations in an S3 bucket.</p><h4>File Upload</h4><p>We can utilize the S3Client object created above for file upload, using the S3Client.send command which accepts a fileObject</p><p>A file object is created using the PutObjectCommand which takes an object as the argument. The object must contain</p><ul><li>Bucket - bucket name</li><li>Key - file name and can be folders if it includes /</li><li>ContentType - file type</li><li>ACL - access control list</li><li>Body - file to be uploaded, can be a string, buffer</li><li>Metadata - object to add custom metadata for the file</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea209effb281f72c67394088804027a6/href">https://medium.com/media/ea209effb281f72c67394088804027a6/href</a></iframe><p>Below is an example code for uploading this file object to AWS</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf6c067169339be476f56a11155e8056/href">https://medium.com/media/cf6c067169339be476f56a11155e8056/href</a></iframe><h4>File Listing</h4><p>For file listing we use ListObjectsV2Command, there is also another command ListObjectsCommand for listing. ListObjectsV2Command is the newer version command. which provides up to 1000 objects with each request. To perform this operation we require read access permission to the bucket.</p><p>You can refer to <a href="https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/listobjectsv2command.html">ListObjectV2Command</a> for details of the input and output object</p><p>For our use case, we used the S3 bucket name to fetch records from it, you can see the code below</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b44a6880969677281660050c3d859f0c/href">https://medium.com/media/b44a6880969677281660050c3d859f0c/href</a></iframe><h4>Accessing the Metadata of Each File</h4><p>To read the metadata of each object we use HeadObjectCommand, this command is helpful only when we are interested in object metadata. This action requires read access permissions to the object.</p><p>To perform this action we also need to add <strong>CORS configurations</strong> to the bucket. In the CORS configuration, you will need to add object metadata keys that are required from the objects you can refer to the <a href="#66e2">CORS JSON</a> mentioned in this section.</p><p>You can refer to <a href="https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/headobjectcommand.html">HeadObjectCommand</a> for more details about input parameters and response.</p><p>You can generally pass Bucket(the bucket name ) and Key(Object/resource Key) that you want to access.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ede4c6390e95cf88f60af0446d63a2d4/href">https://medium.com/media/ede4c6390e95cf88f60af0446d63a2d4/href</a></iframe><p>We are only interested in the Metadata of the object and that’s why we are extracting it from the response</p><pre>const { Metadata } = await s3Client.send(command);</pre><p>For accessing details apart from Metadata you may need to check your S3 bucket roles and permissions</p><p>For the Head response, you will not get any response body. If you are checking the network section for debugging, you will not find any response body but the data is present in response headers.</p><h4>Error Messages</h4><ul><li>If the object does not exist then it will return 404 (“no such key”)</li><li>If the user accessing the S3 object does not have permission then it will return 403 (“access denied”)</li></ul><h3>Conclusion</h3><p>So far we have learned how to create an Open Id connect app in okta, integrate okta in Single Page Application using Client Id and okta organization URL(Auth issuer), integrate okta with AWS by creating new policies and roles, Integrate AWS with react application to upload/ list files from s3 bucket using okta credentials.</p><p>We have built an app that includes end-to-end integration. You can take a reference from this application.</p><p>The source code for this tutorial is available here:</p><p><a href="https://github.com/scalereal/okta-aws-react-app">GitHub - scalereal/okta-aws-react-app: This app includes the integration of okta, aws, and react app. Also includes upload / access s3 bucket resources using okta credentials.</a></p><p><em>Thanks for reading! If you enjoyed reading this article, please </em><strong><em>click the 👏 button and share to help others find it!</em></strong><em> Feel free to leave a comment 💬 below.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c61f5c68f465" width="1" height="1" alt=""><hr><p><a href="https://medium.com/scalereal/manage-aws-s3-files-in-react-front-end-with-the-help-of-okta-securely-c61f5c68f465">Manage AWS S3 files in React front-end with the help of Okta Securely</a> was originally published in <a href="https://medium.com/scalereal">ScaleReal</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>