Giter Site home page Giter Site logo

kentcdodds / netlify-shortener Goto Github PK

View Code? Open in Web Editor NEW
789.0 8.0 76.0 71 KB

Your own free URL shortener with Netlify

Home Page: https://www.youtube.com/watch?v=HL6paXyx6hM&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u

License: MIT License

JavaScript 100.00%

netlify-shortener's Introduction

netlify-shortener

Uses netlify's redirect functionality to make a personal URL shortener. Works beautifully :)


Build Status Code Coverage version downloads MIT License All Contributors PRs Welcome Code of Conduct

The problem

You want a URL shortener for your custom domain and you want an easy way to create and update URLs but you don't want to pay hundreds of dollars a year.

This solution

This relies on Netlify's _redirects file for building a super simple URL shortener where the URLs are managed on GitHub and Netlify handles the redirecting for you.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save netlify-shortener

Usage

Your project should have a _redirects file that looks like this:

/example http://example.com

# fallback
/*       https://your-website.com

This module exposes a binary that you should use in your package.json scripts. You also need to add a homepage to your package.json:

{
  "homepage": "https://jsair.io",
  "scripts": {
    "shorten": "netlify-shortener"
  }
}

Then you can run:

npm run shorten # simply formats your _redirects file
npm run shorten https://yahoo.com # generates a short code and adds it for you
npm run shorten https://github.com gh # adds gh as a short URL for you

The netlify-shortener does a few things:

  1. generates a short code if one is not provided
  2. validates your URL is a real URL
  3. adds the URL to the top of _redirects
  4. runs a git commit and push (this will trigger netlify to deploy your new redirect)
  5. Copies the short URL to your clipboard

Netlify's deploys are normally fast enough that the new URL should be deployed by the time you've shared it to someone.

Shell Function

If you want to be able to run this anywhere in the terminal, you can try making a custom function for your shell.

Shell Agnostic

  1. Add the following executable definition to your package.json:
    {"bin": {"shorten": "cli.js"}}
  2. Create the cli.js file:
    #!/usr/bin/env node
    require('netlify-shortener')
  3. From your project directory, run the following to register the command globally:
    npm link

ScriptKit

You can use this script with ScriptKit which integrates with netlify-shortener as well.

Inspiration

URL shorteners for custom domains and custom short codes are insanely expensive. Hiveam.com was the best price-wise, but the price went up and it's super expensive as well.

Other Solutions

If you know more alternatives, please make a pull request and add it here!

Issues

Looking to contribute? Look for the Good First Issue label.

πŸ› Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior.

See Bugs

πŸ’‘ Feature Requests

Please file an issue to suggest new features. Vote on feature requests by adding a πŸ‘. This helps maintainers prioritize what to work on.

See Feature Requests

Contributors ✨

Thanks goes to these people (emoji key):


Kent C. Dodds

πŸ’» πŸ“– πŸš‡ ⚠️

Carl Rosell

πŸ€”

Phil Hawksworth

πŸ€”

Matt Ferderer

πŸ“–

Jed Fox

πŸ’‘

Eric McCormick

πŸ“–

Preston Lamb

πŸ’»

Daniel D. Beck

πŸ“–

Justin Dorfman

πŸ”

Jake Jarvis

πŸ’»

MichaΓ«l De Boey

πŸ’»

OisΓ­n Quinn

πŸ’» ⚠️

SebJones

πŸ’»

Gabin Aureche

πŸ’» ⚠️

Anton Andreasson

πŸ’»

Marcus Lyons

πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

LICENSE

MIT

netlify-shortener's People

Contributors

allcontributors[bot] avatar andrewmcodes avatar caarlos0 avatar ddbeck avatar edm00se avatar j-f1 avatar jakejarvis avatar jdorfman avatar kentcdodds avatar marcuslyons avatar mattferderer avatar michaeldeboey avatar naton avatar oisinq avatar pjlamb12 avatar sebjones avatar transitive-bullshit avatar zhouzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

netlify-shortener's Issues

Feature request: sorting URLs

As well as formatting the generated URLs list, would it be possible to sort them alphabetically?

This would make it easier when searching through the list to see if there's already an existing short link for a URL.

[Discussion] GUI for netlify-shortener with git-gateway

not a bug, not a feature request, just an idea.

the way NetlifyCMS works is it is a drop-in UI on top of your existing site to help you manage your markdown based content, including creating and merging pull requests for content, all within a GUI. It does this using the Git Gateway. So you never touch your terminal or editor and have a wordpress like experience on your completely static/serverless/git based site.

this strikes me as not too different from the flow needed to edit and manage url shorteners. we just need a GUI that understands _redirects and can link to Git Gateway.

copying @erquhart and @bcomnes who may know more about feasibility. and of course anyone who wants to take this up is super welcome

Windows Subsystem Linux usage

  • netlify-shortener version:1.0.3
  • node version:10.14.2
  • npm (or yarn) version:6.4.1

Relevant code or config

What you did: tried to run shorten for the npm script netlify-shortener

What happened: got the following error output:

/m/c/U/s/g/netlify-short-urls βŽ‡ feat/use-netlify-shortner β—‹
❯ nr shorten                                                                                                                              ⬒ v10.14.2
> [email protected] shorten /mnt/c/Users/spenc/gitrepos/netlify-short-urls 
> netlify-shortener

committing: format links 
On branch feat/use-netlify-shortner 
Your branch is up to date with 'origin/feat/use-netlify-shortner'.

nothing to commit, working tree clean
pushing 
Everything up-to-date 
/mnt/c/Users/spenc/gitrepos/netlify-short-urls/node_modules/clipboardy/lib/linux.js:10 
        throw err;
        ^

Error: xsel: Can't open display: (null) 
: Inappropriate ioctl for device

    at Function.module.exports.sync (/mnt/c/Users/spenc/gitrepos/netlify-short-urls/node_modules/execa/index.js:303:26)
    at Object.copySync (/mnt/c/Users/spenc/gitrepos/netlify-short-urls/node_modules/clipboardy/lib/linux.js:31:18)
    at Object.exports.writeSync.input [as writeSync] (/mnt/c/Users/spenc/gitrepos/netlify-short-urls/node_modules/clipboardy/index.js:38:13)        
    at Object.<anonymous> (/mnt/c/Users/spenc/gitrepos/netlify-short-urls/node_modules/netlify-shortener/dist/index.js:46:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30) 
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
npm ERR! code ELIFECYCLE 
npm ERR! errno 1 
npm ERR! [email protected] shorten: `netlify-shortener` 
npm ERR! Exit status 1
npm ERR!  
npm ERR! Failed at the [email protected] shorten script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: 
npm ERR!     /home/scott/.npm/_logs/2019-01-04T08_19_08_897Z-debug.log

/m/c/U/s/g/netlify-short-urls βŽ‡ feat/use-netlify-shortner β—‹

Reproduction repository: spences10/netlify-short-urls

Problem description: I'm using WSL so not sure if that is an issue or the fact that I'm on a feature branch and not master

Suggested solution: looking into CD settings on netlify now

Documentation on setting up bash alias

You covered setting up bash alias to enable shortening URL by running a command in terminal from anywhere but it's no where to be found here in the documentation yet.

I think adding this to README will vastly increase the value of this module.

[idea] [shortener-list] what about a "shorten ls" command?

Problem description:
My memory is really bad, so I think I will need to check my shortener list when people ask me for stuff. maybe it would be nice to expose a list option so all the shortcodes can be listed on my terminal?

Suggested solution:

run this command:

netlify-shortener ls

will list all the shortcodes available on your _redirects file

foo
bar
baz
example
*

I made a simple prototype but wanted to see if there's any interest before I invest more time on it. :)

It could be also a shortcode for cat _redirects...

Wrong package.json path resolve

  • netlify-shortener 2.1.0:
  • node version: 12.8.1
  • npm (or yarn) version: 6.10.3

Relevant code or config

What you did:

I install the packages as instructed in the README

What happened:

const {
  pkg: {
    baseUrl = 'https://update-baseUrl-in-your-package.json'
  },
  path: pkgPath
} = readPkg.sync({
  cwd: path.join(__dirname, '../..')

path.join(__dirname, '../..') in the above block always refers to <project>/node_modules and as a result I am always getting this error baseUrl = 'https://update-baseUrl-in-your-package.json'

Reproduction repository:

Problem description:

Suggested solution:

[Idea] do fetch before adding new links

Just now I use CLI to add a new link but failed because I forgot to git pull the latest changes as I did some edits over the phone.

I think I will keep forgetting again sometime later, it will be good if we could do a git pull first before starting to add new links, what do you think?

how to get short-url from postman or api

Hi I hope you are doing well,
I need get data from any service I instal and build code and thank you so much for this project but when I try post on ppostman with my netlify url I can't get data there is a wat to make it or I am looking for wrong project.
Thank you for your attention.

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Will the short code collide?

Will there be any possible change that the short code generated be collided? will it generate again until unique with existing short code found in the _redirect file?

Feature request: support for URLs without http/https

Hi! I really like this project.

I think it could be cool to support URLs that don't have http:// or https:// at the start, but would otherwise be valid. I'm used to my browser accepting URLs like that, and it saves a little bit of typing.

Screenshot 2020-05-24 at 15 11 40

I'm happy to give this a go myself if this seems like a relevant feature for this project. I've never contributed to an npm package before, but I'd be happy to give it a try.

Update Docs: Shell Agnostic Method

I've discovered I can use a shell agnostic implementation in place of manually wiring up a shell script in BASH, etc. It makes use of npm link functionality. There's a screen shot below of what's needed. I'm working on a PR to submit that should reflect this in the readme.

Required:

  • a repo (project dir) w/ _redirects file and a package.json with baseUrl and shorten script values

Add:

  • to the package.json:
    "bin": { "shorten": "cli.j.s" }
  • to the project, add cli.js with the following:
    #!/usr/bin/env node
    require('netlify-shortener');

Register:

  • from your project dir, run npm link, which will register your project's bin names against their respective actions, running from your project path

image

Use remains the same, with shorten <url> <name> syntax.

Use package.json's homepage field instead of baseUrl

Problem description:

Currently, netlify-shortener uses its own custom baseUrl field in the package.json. I believe it'd be nice to use an existing convention and avoid creating a new one. I feel like the package.json's homepage fits perfectly for this use case.

(For example, it's what create-react-app uses for generating the app's URL)

Suggested solution:

  1. Add a deprecation message when the user has a baseUrl, encouraging to use homepage instead.
  2. Add support for both homepage and baseUrl, the latter having the priority since it has been the documented way so far.
  3. Release a minor version.
  4. Remove the deprecation message and support for baseUrl.
  5. Release a major version.

What do you think? πŸ˜„ I'm happy to submit a PR if it sounds good.

analytics

"I don't think Netlify will give you analytics"

stay tuned...

(no timeline promised but its a common request)

Netlify CMS Integration

Thank you so much for creating this project. I love it and how quick and simple it is to use. I was wondering if there are plans to add Netlify CMS integration to this project?

Subdomains?

Is it possible to use this with subdomains?

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.