Coder Social home page Coder Social logo

ryanve / speculative Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 6 KB

prerender, preconnect, preload, prefetch, dns-prefetch

Home Page: https://www.npmjs.com/package/speculative

License: MIT License

JavaScript 75.63% HTML 24.37%
javascript prerender prefetch preconnect dns-prefetch preload

speculative's Introduction

speculative

npm install speculative --save
var speculative = require('speculative')

speculative({
  href: 'http://google.com',
  rel: 'prerender'
})

API

speculative(attributes)

attributes

  • href: URI
  • rel: relation
  • pr: probability number between 0 and 1
  • as: content type
  • crossorigin: CORS setting
  • media: media query

rel

rel value Nature Browser action
prerender speculative DNS lookup, TCP/TLS handshake, fetch data, execute scripts
preload mandatory DNS lookup, TCP/TLS handshake, fetch data
prefetch speculative DNS lookup, TCP/TLS handshake, fetch data
preconnect speculative DNS lookup, TCP/TLS handshake
dns-prefetch speculative DNS lookup

Examples

Speculating navigation

speculative({
  href: '/page/2',
  rel: 'prerender next',
  as: 'html',
  pr: .5
})

speculative({
  href: '/page/3',
  rel: 'prefetch',
  as: 'html',
  pr: .2
})

Prefetching playlist tracks

speculative({
  href: './track-2.mp3',
  rel: 'prefetch',
  as: 'media',
  pr: .8
})

speculative({
  href: './track-3.mp3',
  rel: 'prefetch',
  as: 'media',
  pr: .3
})

Prefetching an image

speculative({
  href: './hi-res.jpg',
  rel: 'prefetch',
  media: '(min-width: 60rem)',
  as: 'image'
})

Prefetching JavaScript

speculative({
  href: 'https://code.jquery.com/jquery-3.1.0.slim.min.js',
  rel: 'prefetch',
  crossorigin: 'anonymous',
  as: 'script',
  pr: 1
})

Prefetching CSS

speculative({
  href: '/animations.css',
  rel: 'prefetch',
  media: 'screen',
  as: 'style'
})

Prefetch with care. Test for metered data usage

var cxn = require('cxn')
var speculative = cxn.metered() ? function() {} : require('speculative')

License

MIT

speculative's People

Contributors

ryanve avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

speculative's Issues

Ability To Use Without NPM

Thanks For this Lib!

I was wondering if there is a way to use it without a bundler.
Currently, I get an error when trying link script in an HTML page.
Cannot set property 'speculative' of undefined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.