4

I need to implement snipcart in my angular 2 project. The script tag below needs to be inserted in head of my index.html file.

However, the data-api-key differs for development and production environments.. how do I do that?

<script src="https://cdn.snipcart.com/scripts/2.0/snipcart.js" 
        id="snipcart" 
        data-api-key="insert-your-key-here">
</script>

It is important that the script tag is available in the index.html file, snipcart.com will check that for security purposes.

I have tried to do this at runtime: Add the script tag without the src-url in the index.html file and then in main.ts update the tag attributes with the correct api-key value and src-url.

That way snipcart runs with the correct key, but validation from snipcart.com fails.

So I need to set the api-key at compile time. My index.html needs to be different in development and production mode.

My project is created with angular cli:

"angular-cli": "1.0.0-beta.19-3",

"@angular/common": "~2.1.0",
"@angular/compiler": "~2.1.0",
"@angular/core": "~2.1.0",
"@angular/forms": "~2.1.0",
"@angular/http": "~2.1.0",
"@angular/platform-browser": "~2.1.0",
"@angular/platform-browser-dynamic": "~2.1.0",
"@angular/router": "~3.1.0",

Thanks,

Cheers

Gerd

VinnyG
  • 6,883
  • 7
  • 58
  • 76
Zambiorix
  • 95
  • 6
  • `So I need to set the api-key at compile time. My index.html needs to be different in development and production mode.` I couldn't follow how you come to this conclusion. – Günter Zöchbauer Nov 08 '16 at 15:09
  • snipcart.com loads my website to verify the presence of de script tag (including verification of the src, id and data-api-key attributes). That verification fails if I update my script tag in main.ts. So I have to set the correct api-key from the start, that means, not at runtime... – Zambiorix Nov 08 '16 at 15:18
  • have tried templatizing your index.html ?, meant, using mustache for example, so you generate your index based on template, and you can pass configuration values to the template so index.html will be generated based on those values for example this is for grunt https://www.npmjs.com/package/grunt-mustache-render – asotog Nov 08 '16 at 15:18
  • thanks asotog, I am fairly new to angular and javascript development. I will look into that – Zambiorix Nov 08 '16 at 15:20

1 Answers1

9

You could use Gulp to insert your API Key before deploying your app. You'll need to create a index2.html that will be used to create the real index.html with the good api key at deployment. In index2.html, put SNIPCART_API_KEY where the API key shoud be.

Install Gulp and create gulpfile.js

var gulp = require('gulp');
var package = require('./package.json');
var replace = require('gulp-replace');
var argv = require('yargs').argv;
var gulpif = require('gulp-if');
var rename = require('gulp-rename');

gulp.task('snipcart', function() {
   gulp.src(['index2.html'])
    .pipe(gulpif(argv.production, replace("SNIPCART_API_KEY", package.config.prod.snipcart_api_key)))
    .pipe(gulpif(!argv.production, replace("SNIPCART_API_KEY", package.config.dev.snipcart_api_key)))
    .pipe(rename('index.html'))
    .pipe(gulp.dest('.''))
});

gulp.task('default', ['snipcart']);

This Gulp file should replace the SNIPCART_API_KEY with the good key found in your package.json and create a index.html.

To call your Gulp task when needed, you can add some scripts in your package.json. You'll need to call npm run prod in your deployment process so it creates the index.html with the production key.

package.json

{
 //...
 "scripts": {
    "start": "concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "prod": "gulp default --production",
    "dev": "gulp default"
    //...
  }
 //...
  "config" : { 
    "dev": { "snipcart_api_key" : "YOUR_KEY" },
    "prod": { "snipcart_api_key" : "YOUR_KEY" }
  }
}

You could also create other scripts like "start-dev":"gulp default && npm npm start"

I did not test this solution but you should get the main idea. ;)

I'll edit if I think of something easier & cleaner.

rousseauo
  • 720
  • 7
  • 22
  • Glad it helps ! I'm sure you'll enjoy SnipCart ;) I've talked to their founder about allowing to bootstrap SnipCart (with options) when we want in our app flow instead of at page load with the script tag. Until this is done, I think a setup with Gulp is just fine :) – rousseauo Nov 11 '16 at 03:10
  • 1
    Hopefully they will release an official angular 2 package one day... I love working with Snipcart already :-) Have list 2 pages long compiled with issues and feature requests :-) Would love a digital fulfilment (license keys, download urls, ..) integration, much like FastSpring does... You know the founder? – Zambiorix Nov 11 '16 at 19:24
  • In fact, I know the whole team behind SnipCart. You could be the one making the ng2-snipcart plug-in! That would be a pretty nice thing to have on a resume. ;) https://snipcart.com/plugin-development Don't hesitate to send them your issues/feature requests. – rousseauo Nov 13 '16 at 15:00
  • Hey @rousseauo, I'm not sure what went wrong, but I have sent a rather long e-mail with comments, issues and requests about the integration of SnipCart in an Angular 2 application. I have sent it twice to different e-mail addresses. But they totally ignored my e-mail, never bothered to reply. If you know the team behind Snipcart, can you figure out why? Or provide me with an e-mail address from management? I'm sure they won't agree with ignoring customers .... Thanks! – Zambiorix Dec 03 '16 at 15:56
  • @Zambiorix: You should have received a lengthy email answer by now. :) Seems like the guys had a crazy week! Cheers. – rousseauo Dec 05 '16 at 14:05
  • I did! :-) Thank you for your efforts! By looking at the e-mail, they indeed have been very busy! Thanks again! – Zambiorix Dec 07 '16 at 09:27