npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-url-preview

v1.2.1

Published

Angular UI card component which fetches and displays metadata from a URL (title, description, image and site name).

Downloads

16

Readme

Angular URL Preview

Angular UI card component which fetches and displays metadata from a URL (title, description, image and site name).

The latest stable Angular version this package is built with is 13.0.0

This package uses a proxy server to bypass CORS restriction and mimic a request with an "Access-Control-Allow-Origin" HTTP header present. It behaves similarly to CORS anywhere without the need for an opt-in or a URL whitelist/blacklist.

The Open Graph Protocol is used to scrape the metadata and gather the underlying URL information.

The proxy server also uses Supabase for caching the request responses to achieve instant subsequent metadata loading and Redis to create a request limiter in order to prevent server overloading.

Installation

npm install angular-url-preview

In app.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { AngularUrlPreviewModule } from 'angular-url-preview';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AngularUrlPreviewModule],
bootstrap: [AppComponent]
})
export class AppModule { }

Usage

In the component's template:

<ngx-url-preview url="example.com"></ngx-url-preview>

Specifying the URL is mandatory and it can be in any format, such as:

  • Apex domain: example.com
  • Subdomain: www.example.com
  • With protocol included: https://example.com or https://www.example.com

Customization

displayImage: boolean - Specify whether the image should be shown in the card. Default is true.

customImageSrc: string - Source of the image used in the cards. Source URL can be relative e.g. /assets/image.jpg or an absolute url.

styledFooter: boolean - Specify whether the footer should be styled, which adds a background color and a top border. Default is false.

height: string | number - Height of the card. Can be set in multiple formats, such as for example "400" (string), 400 (number) or "400px". Same rule is valid for the options below.

width: string | number - Width of the card.

imageHeight: string | number - Height of the image in the card. The width of the image adapts to the width of the card.

Additionally, it is possible to specify global styles for the component, which will be valid for every instance of the component without having to repeatedly specify the same configuration. To achieve this, add the .forRoot(config: URLPreviewConfig) configuration to the import of the AngularUrlPreviewModule in your main module. The URLPreviewConfig is a subset of the AngularUrlPreviewComponent and optionally contains all of the fields above. Example:

@NgModule({
...
	imports: [
	...
		AngularUrlPreviewModule.forRoot({ displayImage: false, styledFooter: true, height: 300 })
	]
})

Troubleshooting

It is possible that an error occurs while attempting to scrape the metadata from a website. It's important to note that an error most usually occurs if the target resource is not found or it does not provide the adequate metadata to its client. To check if an error has occurred it's a good idea to take a look at the developer console of your browser.

404 Not found

If this error occurs then firstly verify that the URL you have supplied is accurate and that the webpage it points to exists. If it does and the error persists, then the webpage most likely contains anti-bot protection and does not allow metadata scraping through a proxy server.

Image not loaded

Some websites provide a relative source which points to their banner image in their metadata. This is wrong as it is consequently unable to be found from a different host. Note that this mistake is corrected by angular-url-preview.

If for a different reason the image is not loaded, it is not provided, the resource is forbidden, or it is unsuitable, you may supply a custom image source (either relative or absolute) to the library, which will display the custom image by priority.

Example:

<ngx-url-preview url="example.com" customImageSrc="https://custom.com/logo.png"></ngx-url-preview>

Important: If you are using a relative path for the custom image source, make sure the image is located in the assets folder of your application (most usually src/assets).

Afterwards, you need to specify the assets in angular.json:

...
"architect": {
	"build": {
		"options": {
			...
			"assets": [
				...
				"src/assets"
			],
			...

And then in the component's template:

<ngx-url-preview url="example.com" customImageSrc="/assets/source.jpg"></ngx-url-preview>

Other metadata missing

You should note that this is completely up to the webpage, which has missing or unspecified metadata which was not able to be gathered through the open graph protocol. There are as many defaults and fallbacks as possible handled by angular-url-preview itself.

Slow metadata loading

It is possible that the loading of a website metadata is slow. Note that this will most likely occur only on the first load of the page. Afterwards, a Supabase cache is used to cache the metadata, which will in turn quickly load the metadata on subsequent requests.