Our Tech Journal
  • Introduction
  • Overview
    • Introduction
  • Javascript
    • JS Runtime, Env Context, Scopes, Hoisting & Execution Cycle, var/let/const, this
    • Javascript Runtime Env
      • Event loops
    • What are WebVitals
    • Module Patterns in JS - CommonJS, AMD and ES6 classes
    • Lexical Environment in JS
    • var vs let/const
    • Temporal Dead Zone (TDZ)
    • Execution Context
    • 'this' keyword
    • undefined value in JS
    • Function expressions vs Function statements
    • Javascript Code Execution
    • Closures
    • JS : Object Oriented Programming
    • Prototypal Inheritance
      • Creating Object in Javascript
    • NaN
    • Objects
      • Ways to create
      • ways to check if Object is empty
      • Object's own properties
    • use strict mode
    • typeof vs instanceof
    • Promise
    • localStorage and SessionStorage
    • Event handlers
    • Event bubbling
    • Event Delegation
    • XHR Request
    • Promise Error: Catch vs Error Function in Then
    • Immutability
    • Useful Code samples
    • window, document, screen
    • window.innerheight, outer height, screen.height
    • JS Design Patters
    • Interview Questions
    • Encapsulation - Module Patterns in JS
    • Redesigning services using Entity framework approach
  • JS Functions
    • JS Array - Slice vs Splice
  • PWA
    • What is PWA
  • ES6
    • Sets
    • Maps
    • spread vs destructure operator
  • Web
    • http / https
    • CORS
    • CSRF
    • XSS Attack
    • What is SPA
    • Semantic Elements in HTML
  • Angular 4
    • Angular vs React
    • Change Detection
    • Lazy Loading of modules
    • Preloading
    • AOT Compilation
    • Route Guards
    • Shared Modules
    • Tree Shaking
    • LifeCycle Hooks
    • ngRx
    • Observables
    • Observable vs Subject
      • BehaviorSubject
    • Observables vs Promises
    • Builtin Directives
      • Temp
      • Structural Directives
        • ngForTemp
        • ngSwitch
        • ngFor
        • ngIf
      • Attribute Directives
        • temp
        • ngClass
        • ngStyle
    • Routing
      • Routing in Angular
      • Setting up basic routing
      • routerLink and routerLinkActive
      • router.navigate and ActivatedRoute
      • Route Params as Observables
      • redirectTo
      • relativeTo
      • pathMatch
      • ActivatedRoute
      • Routing in Angular
      • Passing Data with Route
      • Route Parameters
    • Intercept HTTP request
    • Custom Directives
    • Communication between components
    • Angular Modules
    • Reactive Forms
    • Unit Testing
      • TestBed and component fixture
      • Testing HttpClient requests
      • Testing fakeAsync
  • GraphQL
    • Introduction
    • Server
    • Client Side
    • GraphQL in Angular
    • Queries
      • temp
      • query with parameters
      • aliases
      • fragments
      • @include directive
      • @skip directive
      • variables
      • Inline Fragments
  • CSS
    • What is Box model?
    • display: block, inline or inline-block
    • CSS Selector: Combinators
    • CSS Pseudo-classes
    • CSS Pseudo-elements
    • CSS3 Filter
    • CSS3 Transitions
    • Media Queries
    • Flex vs Grid
    • CSS 3 Grids
    • What is Flexbox?
    • position: relative vs absolute
  • SASS
    • Mixins
    • Lists
    • Maps
  • RxJS
    • throttle vs debounceTime
    • distinctUnitChange
    • reduce vs scan
  • Typescript
    • Typeguards
    • Pattern Matching with Typescript
    • TS Decorators
    • Using LINQ in TS
  • NodeJS
    • NodeJS Security Checklist
    • What is Node.js
  • REACT
    • React - VDOM - Under the hood
    • Synthetic events in React
    • Routing - React Router 4
    • React Custom hook
    • Higher-Order Component
    • REDUX
    • Redux Thunk vs Redux Saga
    • forceUpdate()
    • Storing Data in React
    • Error Handling
    • React Context
    • How React-Native works
    • refs
    • Server-side Rendering
    • Jest setup file
    • React-test-renderer
    • Lifecycle
    • React Testing Library
    • React Query
  • JWT
    • What is JWT and How it works
Powered by GitBook
On this page
  • 1. Tracking application state using the URL
  • 2. Tracking application state internally
  • window.location
  • Routers
  • Browser Session and Browser History API
  • Well explained here:
  1. Web

What is SPA

PreviousXSS AttackNextSemantic Elements in HTML

Last updated 5 years ago

SPA = a web application that, instead of having multiple HTML pages for its various parts or functionalities, has only single HTML page where it can render the contents of its various parts in response to navigation actions, without having to make a request to the server to fetch another HTML page.

SPAs can be implemented in two ways :

1. Tracking application state using the URL

In this approach, the application uses some sort of path identifier (pathname) in the URL (window.location) to identify what content is to be rendered on the screen. Application Entry URL-> myapp.com/ User Profile -> myapp.com/user shopping Cart -> myapp.com/cart

Most of the SPA development frameworks use this approach because with this approach the application can start from a state based on the URL and thus, to share some particular content of the website, the URL can be used.

2. Tracking application state internally

In this approach, The URL remains same for all the parts or sections of the application. The application uses some internal state to track what content is to be rendered.

Application Entry URL-> myapp.com/ User Profile -> myapp.com/ shopping Cart -> myapp.com/

This not used much by the frameworks. With this approach, the application always starts from a root state that acts as an entry point to the application and thus to share some particular content that website displays at some later state, you would have to also explain the steps to go to that state from the root state of the website.

window.location

SPA's use the various properties provided by window.location in javascript to figure out what content is to be rendered on the screen.

Routers

SPA frameworks provide some sort of routing feature to allow you to map the contents to be rendered to pathname of the current URL

const appRoutes = [
{ path: '/', component: RootComponent },
{ path: '/user', component: UserProfileComponent },
{ path: '/cart', component: ShoppingCartComponent }
]

The router would keep observing the pathname of the current URL on user's browser and when it notices a change in the URL, it triggers a re-render Additionally , the routers can also support the other properties of window.location like hash and query strings to provide some additional data to further filter or customize the contents.

Browser Session and Browser History API

Browsers maintain a browsing context for each tab:

{
  sessionHistory: 
  [ 
    {
      url: https://www.myapp.com,
      window: {
         document: {
             location: {
             protocol: 'http',
             hostname: 'www.myapp.com',
             pathname: '/user',
             search: '?id=user1',
             hash: '#preferences'
         }
      }
    }, 
    ...,
    ...
  ]


}

Well explained here:

After reading above sections - directly jump to the section - How Browsers Handle Locations explained at

https://medium.com/@pshrmn/demystifying-single-page-applications-3068d0555d46