What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

LOCATION_UPGRADE_CONFIGURATION configures how Angular’s location service works in an application running AngularJS and Angular together. Its LocationUpgradeConfig options select hash- or path-based URLs, control URL encoding, and set the base href for server-side or client-side rendering. For a hybrid app, provide the configuration through LocationUpgradeModule.config() and choose a URL strategy that matches the app’s existing URLs and hosting setup.

What does LocationUpgradeConfig configure?

LocationUpgradeConfig is a stable interface from @angular/common/upgrade. The LOCATION_UPGRADE_CONFIGURATION injection token is the provider token for an object with this configuration shape. Its options are:

  • useHash selects hash-based or path-based location handling.
  • hashPrefix configures the prefix used with hash URLs.
  • urlCodec selects URL encoding and decoding behavior. Angular identifies AngularJSCodec as its default.
  • serverBaseHref configures the base href in a server-side rendered application.
  • appBaseHref configures the base href in a client-side rendered application.

The API reference describes the roles of serverBaseHref and appBaseHref but does not specify their default values. Do not assume they are interchangeable: select the option corresponding to how the application is rendered. Angular’s LocationUpgradeConfig API reference documents the interface and options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do you choose hash or path URLs?

The key difference is where the route appears in the browser URL. PathLocationStrategy uses a normal path, such as http://example.com/foo. HashLocationStrategy keeps the route in the fragment, such as http://example.com/#/foo. Angular’s location strategy documentation describes these URL forms. PathLocationStrategy and HashLocationStrategy

Consideration Path URLs Hash URLs
Example http://example.com/foo http://example.com/#/foo
Hosting behavior The server must return the application for route paths, typically through a fallback or rewrite rule. The route is in the fragment, so the browser does not send that fragment as part of the request path.
Best fit When the server is configured for application routes and path-style URLs match the app’s public links. When fragment-based routes suit the deployment or preserve the existing URL pattern.

There is no single strategy recommended for every deployment. Before choosing path URLs, confirm that direct requests and refreshes on nested routes are served by the application. Also account for established public URLs: changing route style can break links already used by people or other sites. Angular’s router guide discusses location strategies and the production impact of URL choices. Angular router guide: location strategies and browser URL styles

How do you configure it in an AngularJS hybrid app?

Angular’s v17 upgrade guide demonstrates importing LocationUpgradeModule from @angular/common/upgrade and adding its configured module to the Angular application’s AppModule imports. For example, this versioned guide shows useHash: true and hashPrefix: '!' as overrides:

import { LocationUpgradeModule } from '@angular/common/upgrade';

@NgModule({
  imports: [
    LocationUpgradeModule.config({
      useHash: true,
      hashPrefix: '!'
    })
  ]
})
export class AppModule {}

The Angular v17 guide states that useHash defaults to false and hashPrefix defaults to the empty string. Treat these as v17 documentation, not a guarantee for every Angular release; check the guide and API reference for the version your application uses. Angular v17 upgrade guide: LocationUpgradeModule

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep AngularJS code using $location connected

In a hybrid application, LocationUpgradeModule provides a unified location service that moves location responsibility from AngularJS $location to Angular’s Location service. For AngularJS code that still expects $location, the guide demonstrates registering a downgraded $locationShim with downgradeInjectable. The Angular API describes the shim as a drop-in replacement for AngularJS $location. Follow the guide’s example for the exact registration pattern in your hybrid application. Angular upgrade guide: using the Angular location service · LocationUpgradeModule API

Set up location synchronization only when needed

setUpLocationSync is a related helper for synchronizing location changes. Angular documents that it accepts an UpgradeModule and an optional URL type, 'path' or 'hash'. Call it only after UpgradeModule.bootstrap. It installs a location-change listener that triggers history.pushState and addresses the case where onPopState does not trigger history.pushState. setUpLocationSync API reference

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How should you use Angular’s Location service?

For ordinary route changes, Angular recommends using Router.navigate(). Use the Location service when you need to interact with or construct normalized URLs outside normal router navigation. The service normalizes URLs against the application base href, and prepareExternalUrl behaves according to the selected location strategy. Angular Location API reference

Configuration checklist

  • Choose useHash based on the routes people already use and the hosting server’s fallback behavior.
  • If using hash URLs, set hashPrefix only when the desired fragment format requires it.
  • Use urlCodec only when the application needs encoding and decoding behavior other than the documented AngularJSCodec default.
  • Set serverBaseHref for server-side rendered apps or appBaseHref for client-side rendered apps; consult the target release’s documentation for any defaults.
  • In hybrid code, use the documented location module and shim integration, and call setUpLocationSync after bootstrap if the migration architecture requires that synchronization.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.